home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / cxref-1.001 / cxref-1~ / cxref / cpp / cccp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-01  |  264.4 KB  |  9,990 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2.    Copyright (C) 1986, 87, 89, 92, 93, 1994 Free Software Foundation, Inc.
  3.    Written by Paul Rubin, June 1986
  4.    Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6.    Modified by Andrew M. Bishop to provide better input to
  7.    C documentation program `cxref' April-August 1995.
  8.    All AMB hacks are indicated as such in the code.
  9.  
  10. This program is free software; you can redistribute it and/or modify it
  11. under the terms of the GNU General Public License as published by the
  12. Free Software Foundation; either version 2, or (at your option) any
  13. later version.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24.  In other words, you are welcome to use, share and improve this program.
  25.  You are forbidden to forbid anyone else to use, share and improve
  26.  what you give them.   Help stamp out software-hoarding!  */
  27.  
  28. typedef unsigned char U_CHAR;
  29.  
  30. #ifdef EMACS
  31. #define NO_SHORTNAMES
  32. #include "../src/config.h"
  33. #ifdef open
  34. #undef open
  35. #undef read
  36. #undef write
  37. #endif /* open */
  38. #endif /* EMACS */
  39.  
  40. /* The macro EMACS is defined when cpp is distributed as part of Emacs,
  41.    for the sake of machines with limited C compilers.  */
  42. #ifndef EMACS
  43. #include "config.h"
  44. #endif /* not EMACS */
  45.  
  46. #ifndef STANDARD_INCLUDE_DIR
  47. #define STANDARD_INCLUDE_DIR "/usr/include"
  48. #endif
  49.  
  50. #ifndef LOCAL_INCLUDE_DIR
  51. #define LOCAL_INCLUDE_DIR "/usr/local/include"
  52. #endif
  53.  
  54. #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE.  */
  55. #ifdef __STDC__
  56. #define PTR_INT_TYPE ptrdiff_t
  57. #else
  58. #define PTR_INT_TYPE long
  59. #endif
  60. #endif /* 0 */
  61.  
  62. #include "pcp.h"
  63.  
  64. #ifndef STDC_VALUE
  65. #define STDC_VALUE 1
  66. #endif
  67.  
  68. /* By default, colon separates directories in a path.  */
  69. #ifndef PATH_SEPARATOR
  70. #define PATH_SEPARATOR ':'
  71. #endif
  72.  
  73. /* In case config.h defines these.  */
  74. #undef bcopy
  75. #undef bzero
  76. #undef bcmp
  77.  
  78. #include <sys/types.h>
  79. #include <sys/stat.h>
  80. #include <ctype.h>
  81. #include <stdio.h>
  82. #include <signal.h>
  83.  
  84. #ifndef VMS
  85. #ifndef USG
  86. #include <sys/time.h>        /* for __DATE__ and __TIME__ */
  87. #include <sys/resource.h>
  88. #else
  89. #include <time.h>
  90. #include <fcntl.h>
  91. #endif /* USG */
  92. #endif /* not VMS */
  93.  
  94. /* This defines "errno" properly for VMS, and gives us EACCES. */
  95. #include <errno.h>
  96.  
  97. /* VMS-specific definitions */
  98. #ifdef VMS
  99. #include <time.h>
  100. #include <descrip.h>
  101. #define O_RDONLY    0    /* Open arg for Read/Only  */
  102. #define O_WRONLY    1    /* Open arg for Write/Only */
  103. #define read(fd,buf,size)    VMS_read (fd,buf,size)
  104. #define write(fd,buf,size)    VMS_write (fd,buf,size)
  105. #define open(fname,mode,prot)    VMS_open (fname,mode,prot)
  106. #define fopen(fname,mode)    VMS_fopen (fname,mode)
  107. #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
  108. #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt)
  109. static char * VMS_strncat ();
  110. static int VMS_read ();
  111. static int VMS_write ();
  112. static int VMS_open ();
  113. static FILE * VMS_fopen ();
  114. static FILE * VMS_freopen ();
  115. static void hack_vms_include_specification ();
  116. typedef struct { unsigned :16, :16, :16; } vms_ino_t;
  117. #define ino_t vms_ino_t
  118. #define INCLUDE_LEN_FUDGE 10    /* leave room for VMS syntax conversion */
  119. #ifdef __GNUC__
  120. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  121. #endif /* __GNUC__ */
  122. #endif /* VMS */
  123.   
  124. extern char *index ();
  125. extern char *rindex ();
  126.  
  127. #ifndef O_RDONLY
  128. #define O_RDONLY 0
  129. #endif
  130.  
  131. #undef MIN
  132. #undef MAX
  133. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  134. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  135.  
  136. /* Find the largest host integer type and set its size and type.  */
  137.  
  138. #ifndef HOST_BITS_PER_WIDE_INT
  139.  
  140. #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
  141. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
  142. #define HOST_WIDE_INT long
  143. #else
  144. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
  145. #define HOST_WIDE_INT int
  146. #endif
  147.  
  148. #endif
  149.  
  150. #ifndef S_ISREG
  151. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  152. #endif
  153.  
  154. #ifndef S_ISDIR
  155. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  156. #endif
  157.  
  158. /* Define a generic NULL if one hasn't already been defined.  */
  159.  
  160. #ifndef NULL
  161. #define NULL 0
  162. #endif
  163.  
  164. #ifndef GENERIC_PTR
  165. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  166. #define GENERIC_PTR void *
  167. #else
  168. #define GENERIC_PTR char *
  169. #endif
  170. #endif
  171.  
  172. #ifndef NULL_PTR
  173. #define NULL_PTR ((GENERIC_PTR)0)
  174. #endif
  175.  
  176. #ifndef INCLUDE_LEN_FUDGE
  177. #define INCLUDE_LEN_FUDGE 0
  178. #endif
  179.  
  180. /* Forward declarations.  */
  181.  
  182. char *xmalloc ();
  183. void error ();
  184. void warning ();
  185.  
  186. /* External declarations.  */
  187.  
  188. extern char *getenv ();
  189. extern FILE *fdopen ();
  190. extern char *version_string;
  191. extern struct tm *localtime ();
  192. #ifndef VMS
  193. #ifndef HAVE_STRERROR
  194. extern int sys_nerr;
  195. #if defined(bsd4_4) || defined(__NetBSD__)
  196. extern const char *const sys_errlist[];
  197. #else
  198. extern char *sys_errlist[];
  199. #endif
  200. #else    /* HAVE_STERRROR */
  201. char *strerror ();
  202. #endif
  203. #else    /* VMS */
  204. char *strerror (int,...);
  205. #endif
  206. extern int parse_escape ();
  207. extern HOST_WIDE_INT parse_c_expression ();
  208.  
  209. #ifndef errno
  210. extern int errno;
  211. #endif
  212.  
  213. /* Forward declarations.  */
  214.  
  215. struct directive;
  216. struct file_buf;
  217. struct arglist;
  218. struct argdata;
  219.  
  220. #if defined(USG) || defined(VMS)
  221. #ifndef BSTRING
  222. void bcopy ();
  223. void bzero ();
  224. int bcmp ();
  225. #endif
  226. #endif
  227.  
  228. /* These functions are declared to return int instead of void since they
  229.    are going to be placed in a table and some old compilers have trouble with
  230.    pointers to functions returning void.  */
  231.  
  232. static int do_define ();
  233. static int do_line ();
  234. static int do_include ();
  235. static int do_undef ();
  236. static int do_error ();
  237. static int do_pragma ();
  238. static int do_ident ();
  239. static int do_if ();
  240. static int do_xifdef ();
  241. static int do_else ();
  242. static int do_elif ();
  243. static int do_endif ();
  244. static int do_sccs ();
  245. static int do_once ();
  246. static int do_assert ();
  247. static int do_unassert ();
  248. static int do_warning ();
  249.  
  250. static void add_import ();
  251. static void append_include_chain ();
  252. static void deps_output ();
  253. static void make_undef ();
  254. static void make_definition ();
  255. static void make_assertion ();
  256. static void path_include ();
  257. static void initialize_builtins ();
  258. static void initialize_char_syntax ();
  259. static void dump_arg_n ();
  260. static void dump_defn_1 ();
  261. static void delete_macro ();
  262. static void trigraph_pcp ();
  263. static void rescan ();
  264. static void finclude ();
  265. static void validate_else ();
  266. static int comp_def_part ();
  267. static void error_from_errno ();
  268. static void error_with_line ();
  269. void pedwarn ();
  270. void pedwarn_with_line ();
  271. static void pedwarn_with_file_and_line ();
  272. static void fatal ();
  273. void fancy_abort ();
  274. static void pfatal_with_name ();
  275. static void perror_with_name ();
  276. static void pipe_closed ();
  277. static void print_containing_files ();
  278. static int lookup_import ();
  279. static int redundant_include_p ();
  280. static is_system_include ();
  281. static struct file_name_map *read_name_map ();
  282. static char *read_filename_string ();
  283. static int open_include_file ();
  284. static int check_preconditions ();
  285. static void pcfinclude ();
  286. static void pcstring_used ();
  287. static void write_output ();
  288. static int check_macro_name ();
  289. static int compare_defs ();
  290. static int compare_token_lists ();
  291. static HOST_WIDE_INT eval_if_expression ();
  292. static int discard_comments ();
  293. static int change_newlines ();
  294. static int line_for_error ();
  295. static int hashf ();
  296. static int file_size_and_mode ();
  297.  
  298. static struct arglist *read_token_list ();
  299. static void free_token_list ();
  300.  
  301. static struct hashnode *install ();
  302. struct hashnode *lookup ();
  303.  
  304. static struct assertion_hashnode *assertion_install ();
  305. static struct assertion_hashnode *assertion_lookup ();
  306.  
  307. static char *xrealloc ();
  308. static char *xcalloc ();
  309. static char *savestring ();
  310.  
  311. static void delete_assertion ();
  312. static void macroexpand ();
  313. static void dump_all_macros ();
  314. static void conditional_skip ();
  315. static void skip_if_group ();
  316. static void output_line_command ();
  317.  
  318. /* Last arg to output_line_command.  */
  319. enum file_change_code {same_file, enter_file, leave_file};
  320.  
  321. static int grow_outbuf ();
  322. static int handle_directive ();
  323. static void memory_full ();
  324.  
  325. static U_CHAR *macarg1 ();
  326. static char *macarg ();
  327.  
  328. static U_CHAR *skip_to_end_of_comment ();
  329. static U_CHAR *skip_quoted_string ();
  330. static U_CHAR *skip_paren_group ();
  331. static char *quote_string ();
  332.  
  333. static char *check_precompiled ();
  334. /* static struct macrodef create_definition ();    [moved below] */
  335. static void dump_single_macro ();
  336. static void output_dots ();
  337.  
  338. #ifndef FAILURE_EXIT_CODE
  339. #define FAILURE_EXIT_CODE 33    /* gnu cc command understands this */
  340. #endif
  341.  
  342. #ifndef SUCCESS_EXIT_CODE
  343. #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
  344. #endif
  345.  
  346. /* Name under which this program was invoked.  */
  347.  
  348. static char *progname;
  349.  
  350. /* Nonzero means use extra default include directories for C++.  */
  351.  
  352. static int cplusplus;
  353.  
  354. /* Nonzero means handle cplusplus style comments */
  355.  
  356. static int cplusplus_comments;
  357.  
  358. /* Nonzero means handle #import, for objective C.  */
  359.  
  360. static int objc;
  361.  
  362. /* Nonzero means this is an assembly file, and allow
  363.    unknown directives, which could be comments.  */
  364.  
  365. static int lang_asm;
  366.  
  367. /* Current maximum length of directory names in the search path
  368.    for include files.  (Altered as we get more of them.)  */
  369.  
  370. static int max_include_len;
  371.  
  372. /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
  373.  
  374. static int for_lint = 0;
  375.  
  376. /* Nonzero means copy comments into the output file.  */
  377.  
  378. static int put_out_comments = 0;
  379.  
  380. /* Nonzero means don't process the ANSI trigraph sequences.  */
  381.  
  382. static int no_trigraphs = 0;
  383.  
  384. /* Nonzero means print the names of included files rather than
  385.    the preprocessed output.  1 means just the #include "...",
  386.    2 means #include <...> as well.  */
  387.  
  388. static int print_deps = 0;
  389.  
  390. /* Nonzero if missing .h files in -M output are assumed to be generated
  391.    files and not errors.  */
  392.  
  393. static int print_deps_missing_files = 0;
  394.  
  395. /* Nonzero means print names of header files (-H).  */
  396.  
  397. static int print_include_names = 0;
  398.  
  399. /* Nonzero means don't output line number information.  */
  400.  
  401. static int no_line_commands;
  402.  
  403. /* Nonzero means output the text in failing conditionals,
  404.    inside #failed ... #endfailed.  */
  405.  
  406. static int output_conditionals;
  407.  
  408. /* dump_only means inhibit output of the preprocessed text
  409.              and instead output the definitions of all user-defined
  410.              macros in a form suitable for use as input to cccp.
  411.    dump_names means pass #define and the macro name through to output.
  412.    dump_definitions means pass the whole definition (plus #define) through
  413. */
  414.  
  415. static enum {dump_none, dump_only, dump_names, dump_definitions}
  416.      dump_macros = dump_none;
  417.  
  418. /* Nonzero means pass all #define and #undef directives which we actually
  419.    process through to the output stream.  This feature is used primarily
  420.    to allow cc1 to record the #defines and #undefs for the sake of
  421.    debuggers which understand about preprocessor macros, but it may
  422.    also be useful with -E to figure out how symbols are defined, and
  423.    where they are defined.  */
  424. static int debug_output = 0;
  425.  
  426. /* Nonzero indicates special processing used by the pcp program.  The
  427.    special effects of this mode are: 
  428.      
  429.      Inhibit all macro expansion, except those inside #if directives.
  430.  
  431.      Process #define directives normally, and output their contents 
  432.      to the output file.
  433.  
  434.      Output preconditions to pcp_outfile indicating all the relevant
  435.      preconditions for use of this file in a later cpp run.
  436. */
  437. static FILE *pcp_outfile;
  438.  
  439. /* Nonzero means we are inside an IF during a -pcp run.  In this mode
  440.    macro expansion is done, and preconditions are output for all macro
  441.    uses requiring them. */
  442. static int pcp_inside_if;
  443.  
  444. /* Nonzero means never to include precompiled files.
  445.    This is 1 since there's no way now to make precompiled files,
  446.    so it's not worth testing for them.  */
  447. static int no_precomp = 1;
  448.  
  449. /* Nonzero means give all the error messages the ANSI standard requires.  */
  450.  
  451. int pedantic;
  452.  
  453. /* Nonzero means try to make failure to fit ANSI C an error.  */
  454.  
  455. static int pedantic_errors;
  456.  
  457. /* Nonzero means don't print warning messages.  -w.  */
  458.  
  459. static int inhibit_warnings = 0;
  460.  
  461. /* Nonzero means warn if slash-star appears in a comment.  */
  462.  
  463. static int warn_comments;
  464.  
  465. /* Nonzero means warn if a macro argument is (or would be)
  466.    stringified with -traditional.  */
  467.  
  468. static int warn_stringify;
  469.  
  470. /* Nonzero means warn if there are any trigraphs.  */
  471.  
  472. static int warn_trigraphs;
  473.  
  474. /* Nonzero means warn if #import is used.  */
  475.  
  476. static int warn_import = 1;
  477.  
  478. /* Nonzero means turn warnings into errors.  */
  479.  
  480. static int warnings_are_errors;
  481.  
  482. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  483.  
  484. int traditional;
  485.  
  486. /* Nonzero causes output not to be done,
  487.    but directives such as #define that have side effects
  488.    are still obeyed.  */
  489.  
  490. static int no_output;
  491.  
  492. /* Nonzero means this file was included with a -imacros or -include
  493.    command line and should not be recorded as an include file.  */
  494.  
  495. static int no_record_file;
  496.  
  497. /* Nonzero means that we have finished processing the command line options.
  498.    This flag is used to decide whether or not to issue certain errors
  499.    and/or warnings.  */
  500.  
  501. static int done_initializing = 0;
  502.  
  503. /* Line where a newline was first seen in a string constant.  */
  504.  
  505. static int multiline_string_line = 0;
  506.  
  507. /* I/O buffer structure.
  508.    The `fname' field is nonzero for source files and #include files
  509.    and for the dummy text used for -D and -U.
  510.    It is zero for rescanning results of macro expansion
  511.    and for expanding macro arguments.  */
  512. #define INPUT_STACK_MAX 400
  513. static struct file_buf {
  514.   char *fname;
  515.   /* Filename specified with #line command.  */
  516.   char *nominal_fname;
  517.   /* Record where in the search path this file was found.
  518.      For #include_next.  */
  519.   struct file_name_list *dir;
  520.   int lineno;
  521.   int length;
  522.   U_CHAR *buf;
  523.   U_CHAR *bufp;
  524.   /* Macro that this level is the expansion of.
  525.      Included so that we can reenable the macro
  526.      at the end of this level.  */
  527.   struct hashnode *macro;
  528.   /* Value of if_stack at start of this file.
  529.      Used to prohibit unmatched #endif (etc) in an include file.  */
  530.   struct if_stack *if_stack;
  531.   /* Object to be freed at end of input at this level.  */
  532.   U_CHAR *free_ptr;
  533.   /* True if this is a header file included using <FILENAME>.  */
  534.   char system_header_p;
  535. } instack[INPUT_STACK_MAX];
  536.  
  537. static int last_error_tick;       /* Incremented each time we print it.  */
  538. static int input_file_stack_tick;  /* Incremented when the status changes.  */
  539.  
  540. /* Current nesting level of input sources.
  541.    `instack[indepth]' is the level currently being read.  */
  542. static int indepth = -1;
  543. #define CHECK_DEPTH(code) \
  544.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  545.     {                                    \
  546.       error_with_line (line_for_error (instack[indepth].lineno),    \
  547.                "macro or `#include' recursion too deep");    \
  548.       code;                                \
  549.     }
  550.  
  551. /* Current depth in #include directives that use <...>.  */
  552. static int system_include_depth = 0;
  553.  
  554. typedef struct file_buf FILE_BUF;
  555.  
  556. /* The output buffer.  Its LENGTH field is the amount of room allocated
  557.    for the buffer, not the number of chars actually present.  To get
  558.    that, subtract outbuf.buf from outbuf.bufp. */
  559.  
  560. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  561. static FILE_BUF outbuf;
  562.  
  563. /* Grow output buffer OBUF points at
  564.    so it can hold at least NEEDED more chars.  */
  565.  
  566. #define check_expand(OBUF, NEEDED)  \
  567.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  568.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  569.  
  570. struct file_name_list
  571.   {
  572.     struct file_name_list *next;
  573.     char *fname;
  574.     /* If the following is nonzero, it is a macro name.
  575.        Don't include the file again if that macro is defined.  */
  576.     U_CHAR *control_macro;
  577.     /* If the following is nonzero, it is a C-language system include
  578.        directory.  */
  579.     int c_system_include_path;
  580.     /* Mapping of file names for this directory.  */
  581.     struct file_name_map *name_map;
  582.     /* Non-zero if name_map is valid.  */
  583.     int got_name_map;
  584.   };
  585.  
  586. /* #include "file" looks in source file dir, then stack. */
  587. /* #include <file> just looks in the stack. */
  588. /* -I directories are added to the end, then the defaults are added. */
  589. /* The */
  590. static struct default_include {
  591.   char *fname;            /* The name of the directory.  */
  592.   int cplusplus;        /* Only look here if we're compiling C++.  */
  593.   int cxx_aware;        /* Includes in this directory don't need to
  594.                    be wrapped in extern "C" when compiling
  595.                    C++.  */
  596. } include_defaults_array[]
  597. #ifdef INCLUDE_DEFAULTS
  598.   = INCLUDE_DEFAULTS;
  599. #else
  600.   = {
  601.     /* Pick up GNU C++ specific include files.  */
  602.     { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
  603. #ifdef CROSS_COMPILE
  604.     /* This is the dir for fixincludes.  Put it just before
  605.        the files that we fix.  */
  606.     { GCC_INCLUDE_DIR, 0, 0 },
  607.     /* For cross-compilation, this dir name is generated
  608.        automatically in Makefile.in.  */
  609.     { CROSS_INCLUDE_DIR, 0, 0 },
  610.     /* This is another place that the target system's headers might be.  */
  611.     { TOOL_INCLUDE_DIR, 0, 0 },
  612. #else /* not CROSS_COMPILE */
  613.     /* This should be /usr/local/include and should come before
  614.        the fixincludes-fixed header files.  */
  615.     { LOCAL_INCLUDE_DIR, 0, 1 },
  616.     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
  617.        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
  618.     { TOOL_INCLUDE_DIR, 0, 0 },
  619.     /* This is the dir for fixincludes.  Put it just before
  620.        the files that we fix.  */
  621.     { GCC_INCLUDE_DIR, 0, 0 },
  622.     /* Some systems have an extra dir of include files.  */
  623. #ifdef SYSTEM_INCLUDE_DIR
  624.     { SYSTEM_INCLUDE_DIR, 0, 0 },
  625. #endif
  626.     { STANDARD_INCLUDE_DIR, 0, 0 },
  627. #endif /* not CROSS_COMPILE */
  628.     { 0, 0, 0 }
  629.     };
  630. #endif /* no INCLUDE_DEFAULTS */
  631.  
  632. /* The code looks at the defaults through this pointer, rather than through
  633.    the constant structure above.  This pointer gets changed if an environment
  634.    variable specifies other defaults.  */
  635. static struct default_include *include_defaults = include_defaults_array;
  636.  
  637. static struct file_name_list *include = 0;    /* First dir to search */
  638.     /* First dir to search for <file> */
  639. /* This is the first element to use for #include <...>.
  640.    If it is 0, use the entire chain for such includes.  */
  641. static struct file_name_list *first_bracket_include = 0;
  642. /* This is the first element in the chain that corresponds to
  643.    a directory of system header files.  */
  644. static struct file_name_list *first_system_include = 0;
  645. static struct file_name_list *last_include = 0;    /* Last in chain */
  646.  
  647. /* Chain of include directories to put at the end of the other chain.  */
  648. static struct file_name_list *after_include = 0;
  649. static struct file_name_list *last_after_include = 0;    /* Last in chain */
  650.  
  651. /* Chain to put at the start of the system include files.  */
  652. static struct file_name_list *before_system = 0;
  653. static struct file_name_list *last_before_system = 0;    /* Last in chain */
  654.  
  655. /* List of included files that contained #pragma once.  */
  656. static struct file_name_list *dont_repeat_files = 0;
  657.  
  658. /* List of other included files.
  659.    If ->control_macro if nonzero, the file had a #ifndef
  660.    around the entire contents, and ->control_macro gives the macro name.  */
  661. static struct file_name_list *all_include_files = 0;
  662.  
  663. /* Directory prefix that should replace `/usr' in the standard
  664.    include file directories.  */
  665. static char *include_prefix;
  666.  
  667. /* Global list of strings read in from precompiled files.  This list
  668.    is kept in the order the strings are read in, with new strings being
  669.    added at the end through stringlist_tailp.  We use this list to output
  670.    the strings at the end of the run. 
  671. */
  672. static STRINGDEF *stringlist;
  673. static STRINGDEF **stringlist_tailp = &stringlist;
  674.  
  675.  
  676. /* Structure returned by create_definition */
  677. typedef struct macrodef MACRODEF;
  678. struct macrodef
  679. {
  680.   struct definition *defn;
  681.   U_CHAR *symnam;
  682.   int symlen;
  683. };
  684.  
  685. static struct macrodef create_definition ();
  686.  
  687.  
  688. /* Structure allocated for every #define.  For a simple replacement
  689.    such as
  690.        #define foo bar ,
  691.    nargs = -1, the `pattern' list is null, and the expansion is just
  692.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  693.    e.g.,
  694.        #define getchar() getc (stdin) .
  695.    When there are args, the expansion is the replacement text with the
  696.    args squashed out, and the reflist is a list describing how to
  697.    build the output from the input: e.g., "3 chars, then the 1st arg,
  698.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  699.    The chars here come from the expansion.  Whatever is left of the
  700.    expansion after the last arg-occurrence is copied after that arg.
  701.    Note that the reflist can be arbitrarily long---
  702.    its length depends on the number of times the arguments appear in
  703.    the replacement text, not how many args there are.  Example:
  704.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  705.    pattern list
  706.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  707.    where (x, y) means (nchars, argno). */
  708.  
  709. typedef struct definition DEFINITION;
  710. struct definition {
  711.   int nargs;
  712.   int length;            /* length of expansion string */
  713.   int predefined;        /* True if the macro was builtin or */
  714.                 /* came from the command line */
  715.   U_CHAR *expansion;
  716.   int line;            /* Line number of definition */
  717.   char *file;            /* File of definition */
  718.   char rest_args;        /* Nonzero if last arg. absorbs the rest */
  719.   struct reflist {
  720.     struct reflist *next;
  721.     char stringify;        /* nonzero if this arg was preceded by a
  722.                    # operator. */
  723.     char raw_before;        /* Nonzero if a ## operator before arg. */
  724.     char raw_after;        /* Nonzero if a ## operator after arg. */
  725.     char rest_args;        /* Nonzero if this arg. absorbs the rest */
  726.     int nchars;            /* Number of literal chars to copy before
  727.                    this arg occurrence.  */
  728.     int argno;            /* Number of arg to substitute (origin-0) */
  729.   } *pattern;
  730.   union {
  731.     /* Names of macro args, concatenated in reverse order
  732.        with comma-space between them.
  733.        The only use of this is that we warn on redefinition
  734.        if this differs between the old and new definitions.  */
  735.     U_CHAR *argnames;
  736.   } args;
  737. };
  738.  
  739. /* different kinds of things that can appear in the value field
  740.    of a hash node.  Actually, this may be useless now. */
  741. union hashval {
  742.   int ival;
  743.   char *cpval;
  744.   DEFINITION *defn;
  745.   KEYDEF *keydef;
  746. };
  747.  
  748. /*
  749.  * special extension string that can be added to the last macro argument to 
  750.  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
  751.  *         #define wow(a, b...)        process (b, a, b)
  752.  *        { wow (1, 2, 3); }    ->    { process (2, 3, 1, 2, 3); }
  753.  *        { wow (one, two); }    ->    { process (two, one, two); }
  754.  * if this "rest_arg" is used with the concat token '##' and if it is not
  755.  * supplied then the token attached to with ## will not be outputted.  Ex:
  756.  *         #define wow (a, b...)        process (b ## , a, ## b)
  757.  *        { wow (1, 2); }        ->    { process (2, 1, 2); }
  758.  *        { wow (one); }        ->    { process (one); {
  759.  */
  760. static char rest_extension[] = "...";
  761. #define REST_EXTENSION_LENGTH    (sizeof (rest_extension) - 1)
  762.  
  763. /* The structure of a node in the hash table.  The hash table
  764.    has entries for all tokens defined by #define commands (type T_MACRO),
  765.    plus some special tokens like __LINE__ (these each have their own
  766.    type, and the appropriate code is run when that type of node is seen.
  767.    It does not contain control words like "#define", which are recognized
  768.    by a separate piece of code. */
  769.  
  770. /* different flavors of hash nodes --- also used in keyword table */
  771. enum node_type {
  772.  T_DEFINE = 1,    /* the `#define' keyword */
  773.  T_INCLUDE,    /* the `#include' keyword */
  774.  T_INCLUDE_NEXT, /* the `#include_next' keyword */
  775.  T_IMPORT,      /* the `#import' keyword */
  776.  T_IFDEF,    /* the `#ifdef' keyword */
  777.  T_IFNDEF,    /* the `#ifndef' keyword */
  778.  T_IF,        /* the `#if' keyword */
  779.  T_ELSE,    /* `#else' */
  780.  T_PRAGMA,    /* `#pragma' */
  781.  T_ELIF,    /* `#elif' */
  782.  T_UNDEF,    /* `#undef' */
  783.  T_LINE,    /* `#line' */
  784.  T_ERROR,    /* `#error' */
  785.  T_WARNING,    /* `#warning' */
  786.  T_ENDIF,    /* `#endif' */
  787.  T_SCCS,    /* `#sccs', used on system V.  */
  788.  T_IDENT,    /* `#ident', used on system V.  */
  789.  T_ASSERT,    /* `#assert', taken from system V.  */
  790.  T_UNASSERT,    /* `#unassert', taken from system V.  */
  791.  T_SPECLINE,    /* special symbol `__LINE__' */
  792.  T_DATE,    /* `__DATE__' */
  793.  T_FILE,    /* `__FILE__' */
  794.  T_BASE_FILE,    /* `__BASE_FILE__' */
  795.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  796.  T_VERSION,    /* `__VERSION__' */
  797.  T_SIZE_TYPE,   /* `__SIZE_TYPE__' */
  798.  T_PTRDIFF_TYPE,   /* `__PTRDIFF_TYPE__' */
  799.  T_WCHAR_TYPE,   /* `__WCHAR_TYPE__' */
  800.  T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
  801.  T_REGISTER_PREFIX_TYPE,   /* `__REGISTER_PREFIX__' */
  802.  T_TIME,    /* `__TIME__' */
  803.  T_CONST,    /* Constant value, used by `__STDC__' */
  804.  T_MACRO,    /* macro defined by `#define' */
  805.  T_DISABLED,    /* macro temporarily turned off for rescan */
  806.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  807.  T_PCSTRING,    /* precompiled string (hashval is KEYDEF *) */
  808.  T_UNUSED    /* Used for something not defined.  */
  809.  };
  810.  
  811. struct hashnode {
  812.   struct hashnode *next;    /* double links for easy deletion */
  813.   struct hashnode *prev;
  814.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  815.                    chain is kept, in case the node is the head
  816.                    of the chain and gets deleted. */
  817.   enum node_type type;        /* type of special token */
  818.   int length;            /* length of token, for quick comparison */
  819.   U_CHAR *name;            /* the actual name */
  820.   union hashval value;        /* pointer to expansion, or whatever */
  821. };
  822.  
  823. typedef struct hashnode HASHNODE;
  824.  
  825. /* Some definitions for the hash table.  The hash function MUST be
  826.    computed as shown in hashf () below.  That is because the rescan
  827.    loop computes the hash value `on the fly' for most tokens,
  828.    in order to avoid the overhead of a lot of procedure calls to
  829.    the hashf () function.  Hashf () only exists for the sake of
  830.    politeness, for use when speed isn't so important. */
  831.  
  832. #define HASHSIZE 1403
  833. static HASHNODE *hashtab[HASHSIZE];
  834. #define HASHSTEP(old, c) ((old << 2) + c)
  835. #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
  836.  
  837. /* Symbols to predefine.  */
  838.  
  839. #ifdef CPP_PREDEFINES
  840. static char *predefs = CPP_PREDEFINES;
  841. #else
  842. static char *predefs = "";
  843. #endif
  844.  
  845. /* We let tm.h override the types used here, to handle trivial differences
  846.    such as the choice of unsigned int or long unsigned int for size_t.
  847.    When machines start needing nontrivial differences in the size type,
  848.    it would be best to do something here to figure out automatically
  849.    from other information what type to use.  */
  850.  
  851. /* The string value for __SIZE_TYPE__.  */
  852.  
  853. #ifndef SIZE_TYPE
  854. #define SIZE_TYPE "long unsigned int"
  855. #endif
  856.  
  857. /* The string value for __PTRDIFF_TYPE__.  */
  858.  
  859. #ifndef PTRDIFF_TYPE
  860. #define PTRDIFF_TYPE "long int"
  861. #endif
  862.  
  863. /* The string value for __WCHAR_TYPE__.  */
  864.  
  865. #ifndef WCHAR_TYPE
  866. #define WCHAR_TYPE "int"
  867. #endif
  868. char * wchar_type = WCHAR_TYPE;
  869. #undef WCHAR_TYPE
  870.  
  871. /* The string value for __USER_LABEL_PREFIX__ */
  872.  
  873. #ifndef USER_LABEL_PREFIX
  874. #define USER_LABEL_PREFIX ""
  875. #endif
  876.  
  877. /* The string value for __REGISTER_PREFIX__ */
  878.  
  879. #ifndef REGISTER_PREFIX
  880. #define REGISTER_PREFIX ""
  881. #endif
  882.  
  883. /* In the definition of a #assert name, this structure forms
  884.    a list of the individual values asserted.
  885.    Each value is itself a list of "tokens".
  886.    These are strings that are compared by name.  */
  887.  
  888. struct tokenlist_list {
  889.   struct tokenlist_list *next;
  890.   struct arglist *tokens;
  891. };
  892.  
  893. struct assertion_hashnode {
  894.   struct assertion_hashnode *next;    /* double links for easy deletion */
  895.   struct assertion_hashnode *prev;
  896.   /* also, a back pointer to this node's hash
  897.      chain is kept, in case the node is the head
  898.      of the chain and gets deleted. */
  899.   struct assertion_hashnode **bucket_hdr;
  900.   int length;            /* length of token, for quick comparison */
  901.   U_CHAR *name;            /* the actual name */
  902.   /* List of token-sequences.  */
  903.   struct tokenlist_list *value;
  904. };
  905.  
  906. typedef struct assertion_hashnode ASSERTION_HASHNODE;
  907.  
  908. /* Some definitions for the hash table.  The hash function MUST be
  909.    computed as shown in hashf below.  That is because the rescan
  910.    loop computes the hash value `on the fly' for most tokens,
  911.    in order to avoid the overhead of a lot of procedure calls to
  912.    the hashf function.  hashf only exists for the sake of
  913.    politeness, for use when speed isn't so important. */
  914.  
  915. #define ASSERTION_HASHSIZE 37
  916. static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
  917.  
  918. /* Nonzero means inhibit macroexpansion of what seem to be
  919.    assertion tests, in rescan.  For #if.  */
  920. static int assertions_flag;
  921.  
  922. /* `struct directive' defines one #-directive, including how to handle it.  */
  923.  
  924. struct directive {
  925.   int length;            /* Length of name */
  926.   int (*func)();        /* Function to handle directive */
  927.   char *name;            /* Name of directive */
  928.   enum node_type type;        /* Code which describes which directive. */
  929.   char angle_brackets;        /* Nonzero => <...> is special.  */
  930.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  931.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  932. };
  933.  
  934. /* Here is the actual list of #-directives, most-often-used first.  */
  935.  
  936. static struct directive directive_table[] = {
  937.   {  6, do_define, "define", T_DEFINE, 0, 1, 1}, /* An AMB Hack , added ', 1' at end. */
  938.   {  2, do_if, "if", T_IF},
  939.   {  5, do_xifdef, "ifdef", T_IFDEF},
  940.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  941.   {  5, do_endif, "endif", T_ENDIF},
  942.   {  4, do_else, "else", T_ELSE},
  943.   {  4, do_elif, "elif", T_ELIF},
  944.   {  4, do_line, "line", T_LINE},
  945.   {  7, do_include, "include", T_INCLUDE, 1},
  946.   { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
  947.   {  6, do_include, "import", T_IMPORT, 1},
  948.   {  5, do_undef, "undef", T_UNDEF},
  949.   {  5, do_error, "error", T_ERROR},
  950.   {  7, do_warning, "warning", T_WARNING},
  951. #ifdef SCCS_DIRECTIVE
  952.   {  4, do_sccs, "sccs", T_SCCS},
  953. #endif
  954.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  955.   {  5, do_ident, "ident", T_IDENT},
  956.   {  6, do_assert, "assert", T_ASSERT},
  957.   {  8, do_unassert, "unassert", T_UNASSERT},
  958.   {  -1, 0, "", T_UNUSED},
  959. };
  960.  
  961. /* When a directive handler is called,
  962.    this points to the # that started the directive.  */
  963. U_CHAR *directive_start;
  964.  
  965. /* table to tell if char can be part of a C identifier. */
  966. U_CHAR is_idchar[256];
  967. /* table to tell if char can be first char of a c identifier. */
  968. U_CHAR is_idstart[256];
  969. /* table to tell if c is horizontal space.  */
  970. U_CHAR is_hor_space[256];
  971. /* table to tell if c is horizontal or vertical space.  */
  972. static U_CHAR is_space[256];
  973.  
  974. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  975. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  976.   
  977. static int errors = 0;            /* Error counter for exit code */
  978.  
  979. /* Name of output file, for error messages.  */
  980. static char *out_fname;
  981.  
  982. /* Zero means dollar signs are punctuation.
  983.    -$ stores 0; -traditional may store 1.  Default is 1 for VMS, 0 otherwise.
  984.    This must be 0 for correct processing of this ANSI C program:
  985.     #define foo(a) #a
  986.     #define lose(b) foo (b)
  987.     #define test$
  988.     lose (test)    */
  989. static int dollars_in_ident;
  990. #ifndef DOLLARS_IN_IDENTIFIERS
  991. #define DOLLARS_IN_IDENTIFIERS 1
  992. #endif
  993.  
  994. static FILE_BUF expand_to_temp_buffer ();
  995.  
  996. static DEFINITION *collect_expansion ();
  997.  
  998. /* Stack of conditionals currently in progress
  999.    (including both successful and failing conditionals).  */
  1000.  
  1001. struct if_stack {
  1002.   struct if_stack *next;    /* for chaining to the next stack frame */
  1003.   char *fname;        /* copied from input when frame is made */
  1004.   int lineno;            /* similarly */
  1005.   int if_succeeded;        /* true if a leg of this if-group
  1006.                     has been passed through rescan */
  1007.   U_CHAR *control_macro;    /* For #ifndef at start of file,
  1008.                    this is the macro name tested.  */
  1009.   enum node_type type;        /* type of last directive seen in this group */
  1010. };
  1011. typedef struct if_stack IF_STACK_FRAME;
  1012. static IF_STACK_FRAME *if_stack = NULL;
  1013.  
  1014. /* Buffer of -M output.  */
  1015. static char *deps_buffer;
  1016.  
  1017. /* Number of bytes allocated in above.  */
  1018. static int deps_allocated_size;
  1019.  
  1020. /* Number of bytes used.  */
  1021. static int deps_size;
  1022.  
  1023. /* Number of bytes since the last newline.  */
  1024. static int deps_column;
  1025.  
  1026. /* Nonzero means -I- has been seen,
  1027.    so don't look for #include "foo" the source-file directory.  */
  1028. static int ignore_srcdir;
  1029.  
  1030. /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
  1031.    retrying if necessary.  Return a negative value if an error occurs,
  1032.    otherwise return the actual number of bytes read,
  1033.    which must be LEN unless end-of-file was reached.  */
  1034.  
  1035. static int
  1036. safe_read (desc, ptr, len)
  1037.      int desc;
  1038.      char *ptr;
  1039.      int len;
  1040. {
  1041.   int left = len;
  1042.   while (left > 0) {
  1043.     int nchars = read (desc, ptr, left);
  1044.     if (nchars < 0)
  1045.       {
  1046. #ifdef EINTR
  1047.     if (errno == EINTR)
  1048.       continue;
  1049. #endif
  1050.     return nchars;
  1051.       }
  1052.     if (nchars == 0)
  1053.       break;
  1054.     ptr += nchars;
  1055.     left -= nchars;
  1056.   }
  1057.   return len - left;
  1058. }
  1059.  
  1060. /* Write LEN bytes at PTR to descriptor DESC,
  1061.    retrying if necessary, and treating any real error as fatal.  */
  1062.  
  1063. static void
  1064. safe_write (desc, ptr, len)
  1065.      int desc;
  1066.      char *ptr;
  1067.      int len;
  1068. {
  1069.   while (len > 0) {
  1070.     int written = write (desc, ptr, len);
  1071.     if (written < 0)
  1072.       {
  1073. #ifdef EINTR
  1074.     if (errno == EINTR)
  1075.       continue;
  1076. #endif
  1077.     pfatal_with_name (out_fname);
  1078.       }
  1079.     ptr += written;
  1080.     len -= written;
  1081.   }
  1082. }
  1083.  
  1084. int
  1085. main (argc, argv)
  1086.      int argc;
  1087.      char **argv;
  1088. {
  1089.   int st_mode;
  1090.   long st_size;
  1091.   char *in_fname;
  1092.   char *p;
  1093.   int f, i;
  1094.   FILE_BUF *fp;
  1095.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  1096.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  1097.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  1098.   char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
  1099.   char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
  1100.  
  1101.   /* Record the option used with each element of pend_assertions.
  1102.      This is preparation for supporting more than one option for making
  1103.      an assertion.  */
  1104.   char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
  1105.   int inhibit_predefs = 0;
  1106.   int no_standard_includes = 0;
  1107.   int no_standard_cplusplus_includes = 0;
  1108.   int missing_newline = 0;
  1109.  
  1110.   /* Non-0 means don't output the preprocessed program.  */
  1111.   int inhibit_output = 0;
  1112.   /* Non-0 means -v, so print the full set of include dirs.  */
  1113.   int verbose = 0;
  1114.  
  1115.   /* File name which deps are being written to.
  1116.      This is 0 if deps are being written to stdout.  */
  1117.   char *deps_file = 0;
  1118.   /* Fopen file mode to open deps_file with.  */
  1119.   char *deps_mode = "a";
  1120.   /* Stream on which to print the dependency information.  */
  1121.   FILE *deps_stream = 0;
  1122.   /* Target-name to write with the dependency information.  */
  1123.   char *deps_target = 0;
  1124.  
  1125. #ifdef RLIMIT_STACK
  1126.   /* Get rid of any avoidable limit on stack size.  */
  1127.   {
  1128.     struct rlimit rlim;
  1129.  
  1130.     /* Set the stack limit huge so that alloca (particularly stringtab
  1131.      * in dbxread.c) does not fail. */
  1132.     getrlimit (RLIMIT_STACK, &rlim);
  1133.     rlim.rlim_cur = rlim.rlim_max;
  1134.     setrlimit (RLIMIT_STACK, &rlim);
  1135.   }
  1136. #endif /* RLIMIT_STACK defined */
  1137.  
  1138. #ifdef SIGPIPE
  1139.   signal (SIGPIPE, pipe_closed);
  1140. #endif
  1141.  
  1142.   p = argv[0] + strlen (argv[0]);
  1143.   while (p != argv[0] && p[-1] != '/'
  1144. #ifdef DIR_SEPARATOR
  1145.      && p[-1] != DIR_SEPARATOR
  1146. #endif
  1147.      )
  1148.     --p;
  1149.   progname = p;
  1150.  
  1151. #ifdef VMS
  1152.   {
  1153.     /* Remove directories from PROGNAME.  */
  1154.     char *s;
  1155.  
  1156.     progname = savestring (argv[0]);
  1157.  
  1158.     if (!(s = rindex (progname, ']')))
  1159.       s = rindex (progname, ':');
  1160.     if (s)
  1161.       strcpy (progname, s+1);
  1162.     if (s = rindex (progname, '.'))
  1163.       *s = '\0';
  1164.   }
  1165. #endif
  1166.  
  1167.   in_fname = NULL;
  1168.   out_fname = NULL;
  1169.  
  1170.   /* Initialize is_idchar to allow $.  */
  1171.   dollars_in_ident = 1;
  1172.   initialize_char_syntax ();
  1173.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
  1174.  
  1175.   no_line_commands = 0;
  1176.   no_trigraphs = 1;
  1177.   dump_macros = dump_none;
  1178.   no_output = 0;
  1179.   cplusplus = 0;
  1180.   cplusplus_comments = 0;
  1181.  
  1182.   bzero ((char *) pend_files, argc * sizeof (char *));
  1183.   bzero ((char *) pend_defs, argc * sizeof (char *));
  1184.   bzero ((char *) pend_undefs, argc * sizeof (char *));
  1185.   bzero ((char *) pend_assertions, argc * sizeof (char *));
  1186.   bzero ((char *) pend_includes, argc * sizeof (char *));
  1187.  
  1188.   /* Process switches and find input file name.  */
  1189.  
  1190.   for (i = 1; i < argc; i++) {
  1191.     if (argv[i][0] != '-') {
  1192.       if (out_fname != NULL)
  1193.     fatal ("Usage: %s [switches] input output", argv[0]);
  1194.       else if (in_fname != NULL)
  1195.     out_fname = argv[i];
  1196.       else
  1197.     in_fname = argv[i];
  1198.     } else {
  1199.       switch (argv[i][1]) {
  1200.  
  1201.       case 'i':
  1202.     if (!strcmp (argv[i], "-include")) {
  1203.       if (i + 1 == argc)
  1204.         fatal ("Filename missing after `-include' option");
  1205.       else
  1206.         pend_includes[i] = argv[i+1], i++;
  1207.     }
  1208.     if (!strcmp (argv[i], "-imacros")) {
  1209.       if (i + 1 == argc)
  1210.         fatal ("Filename missing after `-imacros' option");
  1211.       else
  1212.         pend_files[i] = argv[i+1], i++;
  1213.     }
  1214.     if (!strcmp (argv[i], "-iprefix")) {
  1215.       if (i + 1 == argc)
  1216.         fatal ("Filename missing after `-iprefix' option");
  1217.       else
  1218.         include_prefix = argv[++i];
  1219.     }
  1220.     if (!strcmp (argv[i], "-ifoutput")) {
  1221.       output_conditionals = 1;
  1222.     }
  1223.     if (!strcmp (argv[i], "-isystem")) {
  1224.       struct file_name_list *dirtmp;
  1225.  
  1226.       if (i + 1 == argc)
  1227.         fatal ("Filename missing after `-isystem' option");
  1228.  
  1229.       dirtmp = (struct file_name_list *)
  1230.         xmalloc (sizeof (struct file_name_list));
  1231.       dirtmp->next = 0;
  1232.       dirtmp->control_macro = 0;
  1233.       dirtmp->c_system_include_path = 1;
  1234.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
  1235.       strcpy (dirtmp->fname, argv[++i]);
  1236.       dirtmp->got_name_map = 0;
  1237.  
  1238.       if (before_system == 0)
  1239.         before_system = dirtmp;
  1240.       else
  1241.         last_before_system->next = dirtmp;
  1242.       last_before_system = dirtmp; /* Tail follows the last one */
  1243.     }
  1244.     /* Add directory to end of path for includes,
  1245.        with the default prefix at the front of its name.  */
  1246.     if (!strcmp (argv[i], "-iwithprefix")) {
  1247.       struct file_name_list *dirtmp;
  1248.       char *prefix;
  1249.  
  1250.       if (include_prefix != 0)
  1251.         prefix = include_prefix;
  1252.       else {
  1253.         prefix = savestring (GCC_INCLUDE_DIR);
  1254.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1255.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1256.           prefix[strlen (prefix) - 7] = 0;
  1257.       }
  1258.  
  1259.       dirtmp = (struct file_name_list *)
  1260.         xmalloc (sizeof (struct file_name_list));
  1261.       dirtmp->next = 0;    /* New one goes on the end */
  1262.       dirtmp->control_macro = 0;
  1263.       dirtmp->c_system_include_path = 0;
  1264.       if (i + 1 == argc)
  1265.         fatal ("Directory name missing after `-iwithprefix' option");
  1266.  
  1267.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
  1268.                         + strlen (prefix) + 1);
  1269.       strcpy (dirtmp->fname, prefix);
  1270.       strcat (dirtmp->fname, argv[++i]);
  1271.       dirtmp->got_name_map = 0;
  1272.  
  1273.       if (after_include == 0)
  1274.         after_include = dirtmp;
  1275.       else
  1276.         last_after_include->next = dirtmp;
  1277.       last_after_include = dirtmp; /* Tail follows the last one */
  1278.     }
  1279.     /* Add directory to main path for includes,
  1280.        with the default prefix at the front of its name.  */
  1281.     if (!strcmp (argv[i], "-iwithprefixbefore")) {
  1282.       struct file_name_list *dirtmp;
  1283.       char *prefix;
  1284.  
  1285.       if (include_prefix != 0)
  1286.         prefix = include_prefix;
  1287.       else {
  1288.         prefix = savestring (GCC_INCLUDE_DIR);
  1289.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1290.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1291.           prefix[strlen (prefix) - 7] = 0;
  1292.       }
  1293.  
  1294.       dirtmp = (struct file_name_list *)
  1295.         xmalloc (sizeof (struct file_name_list));
  1296.       dirtmp->next = 0;    /* New one goes on the end */
  1297.       dirtmp->control_macro = 0;
  1298.       dirtmp->c_system_include_path = 0;
  1299.       if (i + 1 == argc)
  1300.         fatal ("Directory name missing after `-iwithprefixbefore' option");
  1301.  
  1302.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
  1303.                         + strlen (prefix) + 1);
  1304.       strcpy (dirtmp->fname, prefix);
  1305.       strcat (dirtmp->fname, argv[++i]);
  1306.       dirtmp->got_name_map = 0;
  1307.  
  1308.       append_include_chain (dirtmp, dirtmp);
  1309.     }
  1310.     /* Add directory to end of path for includes.  */
  1311.     if (!strcmp (argv[i], "-idirafter")) {
  1312.       struct file_name_list *dirtmp;
  1313.  
  1314.       dirtmp = (struct file_name_list *)
  1315.         xmalloc (sizeof (struct file_name_list));
  1316.       dirtmp->next = 0;    /* New one goes on the end */
  1317.       dirtmp->control_macro = 0;
  1318.       dirtmp->c_system_include_path = 0;
  1319.       if (i + 1 == argc)
  1320.         fatal ("Directory name missing after `-idirafter' option");
  1321.       else
  1322.         dirtmp->fname = argv[++i];
  1323.       dirtmp->got_name_map = 0;
  1324.  
  1325.       if (after_include == 0)
  1326.         after_include = dirtmp;
  1327.       else
  1328.         last_after_include->next = dirtmp;
  1329.       last_after_include = dirtmp; /* Tail follows the last one */
  1330.     }
  1331.     break;
  1332.  
  1333.       case 'o':
  1334.     if (out_fname != NULL)
  1335.       fatal ("Output filename specified twice");
  1336.     if (i + 1 == argc)
  1337.       fatal ("Filename missing after -o option");
  1338.     out_fname = argv[++i];
  1339.     if (!strcmp (out_fname, "-"))
  1340.       out_fname = "";
  1341.     break;
  1342.  
  1343.       case 'p':
  1344.     if (!strcmp (argv[i], "-pedantic"))
  1345.       pedantic = 1;
  1346.     else if (!strcmp (argv[i], "-pedantic-errors")) {
  1347.       pedantic = 1;
  1348.       pedantic_errors = 1;
  1349.     } else if (!strcmp (argv[i], "-pcp")) {
  1350.       char *pcp_fname;
  1351.       if (i + 1 == argc)
  1352.         fatal ("Filename missing after -pcp option");
  1353.       pcp_fname = argv[++i];
  1354.       pcp_outfile = 
  1355.         ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
  1356.          ? fopen (pcp_fname, "w")
  1357.          : fdopen (dup (fileno (stdout)), "w"));
  1358.       if (pcp_outfile == 0)
  1359.         pfatal_with_name (pcp_fname);
  1360.       no_precomp = 1;
  1361.     }
  1362.     break;
  1363.  
  1364.       case 't':
  1365.     if (!strcmp (argv[i], "-traditional")) {
  1366.       traditional = 1;
  1367.       if (dollars_in_ident > 0)
  1368.         dollars_in_ident = 1;
  1369.     } else if (!strcmp (argv[i], "-trigraphs")) {
  1370.       no_trigraphs = 0;
  1371.     }
  1372.     break;
  1373.  
  1374.       case 'l':
  1375.     if (! strcmp (argv[i], "-lang-c"))
  1376.       cplusplus = 0, cplusplus_comments = 0, objc = 0;
  1377.     if (! strcmp (argv[i], "-lang-c++"))
  1378.       cplusplus = 1, cplusplus_comments = 1, objc = 0;
  1379.     if (! strcmp (argv[i], "-lang-c-c++-comments"))
  1380.       cplusplus = 0, cplusplus_comments = 1, objc = 0;
  1381.     if (! strcmp (argv[i], "-lang-objc"))
  1382.       objc = 1, cplusplus = 0, cplusplus_comments = 1;
  1383.     if (! strcmp (argv[i], "-lang-objc++"))
  1384.       objc = 1, cplusplus = 1, cplusplus_comments = 1;
  1385.      if (! strcmp (argv[i], "-lang-asm"))
  1386.        lang_asm = 1;
  1387.      if (! strcmp (argv[i], "-lint"))
  1388.        for_lint = 1;
  1389.     break;
  1390.  
  1391.       case '+':
  1392.     cplusplus = 1, cplusplus_comments = 1;
  1393.     break;
  1394.  
  1395.       case 'w':
  1396.     inhibit_warnings = 1;
  1397.     break;
  1398.  
  1399.       case 'W':
  1400.     if (!strcmp (argv[i], "-Wtrigraphs"))
  1401.       warn_trigraphs = 1;
  1402.     else if (!strcmp (argv[i], "-Wno-trigraphs"))
  1403.       warn_trigraphs = 0;
  1404.     else if (!strcmp (argv[i], "-Wcomment"))
  1405.       warn_comments = 1;
  1406.     else if (!strcmp (argv[i], "-Wno-comment"))
  1407.       warn_comments = 0;
  1408.     else if (!strcmp (argv[i], "-Wcomments"))
  1409.       warn_comments = 1;
  1410.     else if (!strcmp (argv[i], "-Wno-comments"))
  1411.       warn_comments = 0;
  1412.     else if (!strcmp (argv[i], "-Wtraditional"))
  1413.       warn_stringify = 1;
  1414.     else if (!strcmp (argv[i], "-Wno-traditional"))
  1415.       warn_stringify = 0;
  1416.     else if (!strcmp (argv[i], "-Wimport"))
  1417.       warn_import = 1;
  1418.     else if (!strcmp (argv[i], "-Wno-import"))
  1419.       warn_import = 0;
  1420.     else if (!strcmp (argv[i], "-Werror"))
  1421.       warnings_are_errors = 1;
  1422.     else if (!strcmp (argv[i], "-Wno-error"))
  1423.       warnings_are_errors = 0;
  1424.     else if (!strcmp (argv[i], "-Wall"))
  1425.       {
  1426.         warn_trigraphs = 1;
  1427.         warn_comments = 1;
  1428.       }
  1429.     break;
  1430.  
  1431.       case 'M':
  1432.     /* The style of the choices here is a bit mixed.
  1433.        The chosen scheme is a hybrid of keeping all options in one string
  1434.        and specifying each option in a separate argument:
  1435.        -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
  1436.        -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
  1437.        -M[M][G][D file].  This is awkward to handle in specs, and is not
  1438.        as extensible.  */
  1439.     /* ??? -MG must be specified in addition to one of -M or -MM.
  1440.        This can be relaxed in the future without breaking anything.
  1441.        The converse isn't true.  */
  1442.  
  1443.     /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
  1444.     if (!strcmp (argv[i], "-MG"))
  1445.       {
  1446.         print_deps_missing_files = 1;
  1447.         break;
  1448.       }
  1449.     if (!strcmp (argv[i], "-M"))
  1450.       print_deps = 2;
  1451.     else if (!strcmp (argv[i], "-MM"))
  1452.       print_deps = 1;
  1453.     else if (!strcmp (argv[i], "-MD"))
  1454.       print_deps = 2;
  1455.     else if (!strcmp (argv[i], "-MMD"))
  1456.       print_deps = 1;
  1457.     /* For -MD and -MMD options, write deps on file named by next arg.  */
  1458.     if (!strcmp (argv[i], "-MD")
  1459.         || !strcmp (argv[i], "-MMD")) {
  1460.       if (i + 1 == argc)
  1461.         fatal ("Filename missing after %s option", argv[i]);
  1462.       i++;
  1463.       deps_file = argv[i];
  1464.       deps_mode = "w";
  1465.     } else {
  1466.       /* For -M and -MM, write deps on standard output
  1467.          and suppress the usual output.  */
  1468.       deps_stream = stdout;
  1469.       inhibit_output = 1;
  1470.     }      
  1471.     break;
  1472.  
  1473.       case 'd':
  1474.     {
  1475.       char *p = argv[i] + 2;
  1476.       char c;
  1477.       while (c = *p++) {
  1478.         /* Arg to -d specifies what parts of macros to dump */
  1479.         switch (c) {
  1480.         case 'M':
  1481.           dump_macros = dump_only;
  1482.           no_output = 1;
  1483.           break;
  1484.         case 'N':
  1485.           dump_macros = dump_names;
  1486.           break;
  1487.         case 'D':
  1488.           dump_macros = dump_definitions;
  1489.           break;
  1490.         }
  1491.       }
  1492.     }
  1493.     break;
  1494.  
  1495.       case 'g':
  1496.     if (argv[i][2] == '3')
  1497.       debug_output = 1;
  1498.     break;
  1499.  
  1500.       case 'v':
  1501.     fprintf (stderr, "GNU CPP version %s", version_string);
  1502. #ifdef TARGET_VERSION
  1503.     TARGET_VERSION;
  1504. #endif
  1505.     fprintf (stderr, "\n");
  1506.     verbose = 1;
  1507.     break;
  1508.  
  1509.       case 'H':
  1510.     print_include_names = 1;
  1511.     break;
  1512.  
  1513.       case 'D':
  1514.     if (argv[i][2] != 0)
  1515.       pend_defs[i] = argv[i] + 2;
  1516.     else if (i + 1 == argc)
  1517.       fatal ("Macro name missing after -D option");
  1518.     else
  1519.       i++, pend_defs[i] = argv[i];
  1520.     break;
  1521.  
  1522.       case 'A':
  1523.     {
  1524.       char *p;
  1525.  
  1526.       if (argv[i][2] != 0)
  1527.         p = argv[i] + 2;
  1528.       else if (i + 1 == argc)
  1529.         fatal ("Assertion missing after -A option");
  1530.       else
  1531.         p = argv[++i];
  1532.  
  1533.       if (!strcmp (p, "-")) {
  1534.         /* -A- eliminates all predefined macros and assertions.
  1535.            Let's include also any that were specified earlier
  1536.            on the command line.  That way we can get rid of any
  1537.            that were passed automatically in from GCC.  */
  1538.         int j;
  1539.         inhibit_predefs = 1;
  1540.         for (j = 0; j < i; j++)
  1541.           pend_defs[j] = pend_assertions[j] = 0;
  1542.       } else {
  1543.         pend_assertions[i] = p;
  1544.         pend_assertion_options[i] = "-A";
  1545.       }
  1546.     }
  1547.     break;
  1548.  
  1549.       case 'U':        /* JF #undef something */
  1550.     if (argv[i][2] != 0)
  1551.       pend_undefs[i] = argv[i] + 2;
  1552.     else if (i + 1 == argc)
  1553.       fatal ("Macro name missing after -U option");
  1554.     else
  1555.       pend_undefs[i] = argv[i+1], i++;
  1556.     break;
  1557.  
  1558.       case 'C':
  1559.     put_out_comments = 1;
  1560.     break;
  1561.  
  1562.       case 'E':            /* -E comes from cc -E; ignore it.  */
  1563.     break;
  1564.  
  1565.       case 'P':
  1566.     no_line_commands = 1;
  1567.     break;
  1568.  
  1569.       case '$':            /* Don't include $ in identifiers.  */
  1570.     dollars_in_ident = 0;
  1571.     break;
  1572.  
  1573.       case 'I':            /* Add directory to path for includes.  */
  1574.     {
  1575.       struct file_name_list *dirtmp;
  1576.  
  1577.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1578.         ignore_srcdir = 1;
  1579.         /* Don't use any preceding -I directories for #include <...>.  */
  1580.         first_bracket_include = 0;
  1581.       }
  1582.       else {
  1583.         dirtmp = (struct file_name_list *)
  1584.           xmalloc (sizeof (struct file_name_list));
  1585.         dirtmp->next = 0;        /* New one goes on the end */
  1586.         dirtmp->control_macro = 0;
  1587.         dirtmp->c_system_include_path = 0;
  1588.         if (argv[i][2] != 0)
  1589.           dirtmp->fname = argv[i] + 2;
  1590.         else if (i + 1 == argc)
  1591.           fatal ("Directory name missing after -I option");
  1592.         else
  1593.           dirtmp->fname = argv[++i];
  1594.         dirtmp->got_name_map = 0;
  1595.         append_include_chain (dirtmp, dirtmp);
  1596.       }
  1597.     }
  1598.     break;
  1599.  
  1600.       case 'n':
  1601.     if (!strcmp (argv[i], "-nostdinc"))
  1602.       /* -nostdinc causes no default include directories.
  1603.          You must specify all include-file directories with -I.  */
  1604.       no_standard_includes = 1;
  1605.     else if (!strcmp (argv[i], "-nostdinc++"))
  1606.       /* -nostdinc++ causes no default C++-specific include directories. */
  1607.       no_standard_cplusplus_includes = 1;
  1608.     else if (!strcmp (argv[i], "-noprecomp"))
  1609.       no_precomp = 1;
  1610.     break;
  1611.  
  1612.       case 'u':
  1613.     /* Sun compiler passes undocumented switch "-undef".
  1614.        Let's assume it means to inhibit the predefined symbols.  */
  1615.     inhibit_predefs = 1;
  1616.     break;
  1617.  
  1618.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  1619.     if (in_fname == NULL) {
  1620.       in_fname = "";
  1621.       break;
  1622.     } else if (out_fname == NULL) {
  1623.       out_fname = "";
  1624.       break;
  1625.     }    /* else fall through into error */
  1626.  
  1627.       default:
  1628.     fatal ("Invalid option `%s'", argv[i]);
  1629.       }
  1630.     }
  1631.   }
  1632.  
  1633.   /* Add dirs from CPATH after dirs from -I.  */
  1634.   /* There seems to be confusion about what CPATH should do,
  1635.      so for the moment it is not documented.  */
  1636.   /* Some people say that CPATH should replace the standard include dirs,
  1637.      but that seems pointless: it comes before them, so it overrides them
  1638.      anyway.  */
  1639. #ifdef WINNT
  1640.   p = (char *) getenv ("Include");
  1641. #else
  1642.   p = (char *) getenv ("CPATH");
  1643. #endif
  1644.   if (p != 0 && ! no_standard_includes)
  1645.     path_include (p);
  1646.  
  1647.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  1648.   initialize_char_syntax ();
  1649.  
  1650.   /* Initialize output buffer */
  1651.  
  1652.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  1653.   outbuf.bufp = outbuf.buf;
  1654.   outbuf.length = OUTBUF_SIZE;
  1655.  
  1656.   /* Do partial setup of input buffer for the sake of generating
  1657.      early #line directives (when -g is in effect).  */
  1658.  
  1659.   fp = &instack[++indepth];
  1660.   if (in_fname == NULL)
  1661.     in_fname = "";
  1662.   fp->nominal_fname = fp->fname = in_fname;
  1663.   fp->lineno = 0;
  1664.  
  1665.   /* In C++, wchar_t is a distinct basic type, and we can expect
  1666.      __wchar_t to be defined by cc1plus.  */
  1667.   if (cplusplus)
  1668.     wchar_type = "__wchar_t";
  1669.  
  1670.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  1671.      and option processing.  */
  1672.   initialize_builtins (fp, &outbuf);
  1673.  
  1674.   /* Do standard #defines and assertions
  1675.      that identify system and machine type.  */
  1676.  
  1677.   if (!inhibit_predefs) {
  1678.     char *p = (char *) alloca (strlen (predefs) + 1);
  1679.     strcpy (p, predefs);
  1680.     while (*p) {
  1681.       char *q;
  1682.       while (*p == ' ' || *p == '\t')
  1683.     p++;
  1684.       /* Handle -D options.  */ 
  1685.       if (p[0] == '-' && p[1] == 'D') {
  1686.     q = &p[2];
  1687.     while (*p && *p != ' ' && *p != '\t')
  1688.       p++;
  1689.     if (*p != 0)
  1690.       *p++= 0;
  1691.     if (debug_output)
  1692.       output_line_command (fp, &outbuf, 0, same_file);
  1693.     make_definition (q, &outbuf);
  1694.     while (*p == ' ' || *p == '\t')
  1695.       p++;
  1696.       } else if (p[0] == '-' && p[1] == 'A') {
  1697.     /* Handle -A options (assertions).  */ 
  1698.     char *assertion;
  1699.     char *past_name;
  1700.     char *value;
  1701.     char *past_value;
  1702.     char *termination;
  1703.     int save_char;
  1704.  
  1705.     assertion = &p[2];
  1706.     past_name = assertion;
  1707.     /* Locate end of name.  */
  1708.     while (*past_name && *past_name != ' '
  1709.            && *past_name != '\t' && *past_name != '(')
  1710.       past_name++;
  1711.     /* Locate `(' at start of value.  */
  1712.     value = past_name;
  1713.     while (*value && (*value == ' ' || *value == '\t'))
  1714.       value++;
  1715.     if (*value++ != '(')
  1716.       abort ();
  1717.     while (*value && (*value == ' ' || *value == '\t'))
  1718.       value++;
  1719.     past_value = value;
  1720.     /* Locate end of value.  */
  1721.     while (*past_value && *past_value != ' '
  1722.            && *past_value != '\t' && *past_value != ')')
  1723.       past_value++;
  1724.     termination = past_value;
  1725.     while (*termination && (*termination == ' ' || *termination == '\t'))
  1726.       termination++;
  1727.     if (*termination++ != ')')
  1728.       abort ();
  1729.     if (*termination && *termination != ' ' && *termination != '\t')
  1730.       abort ();
  1731.     /* Temporarily null-terminate the value.  */
  1732.     save_char = *termination;
  1733.     *termination = '\0';
  1734.     /* Install the assertion.  */
  1735.     make_assertion ("-A", assertion);
  1736.     *termination = (char) save_char;
  1737.     p = termination;
  1738.     while (*p == ' ' || *p == '\t')
  1739.       p++;
  1740.       } else {
  1741.     abort ();
  1742.       }
  1743.     }
  1744.   }
  1745.  
  1746.   /* Now handle the command line options.  */
  1747.  
  1748.   /* Do -U's, -D's and -A's in the order they were seen.  */
  1749.   for (i = 1; i < argc; i++) {
  1750.     if (pend_undefs[i]) {
  1751.       if (debug_output)
  1752.         output_line_command (fp, &outbuf, 0, same_file);
  1753.       make_undef (pend_undefs[i], &outbuf);
  1754.     }
  1755.     if (pend_defs[i]) {
  1756.       if (debug_output)
  1757.         output_line_command (fp, &outbuf, 0, same_file);
  1758.       make_definition (pend_defs[i], &outbuf);
  1759.     }
  1760.     if (pend_assertions[i])
  1761.       make_assertion (pend_assertion_options[i], pend_assertions[i]);
  1762.   }
  1763.  
  1764.   done_initializing = 1;
  1765.  
  1766.   { /* read the appropriate environment variable and if it exists
  1767.        replace include_defaults with the listed path. */
  1768.     char *epath = 0;
  1769.     switch ((objc << 1) + cplusplus)
  1770.       {
  1771.       case 0:
  1772.     epath = getenv ("C_INCLUDE_PATH");
  1773.     break;
  1774.       case 1:
  1775.     epath = getenv ("CPLUS_INCLUDE_PATH");
  1776.     break;
  1777.       case 2:
  1778.     epath = getenv ("OBJC_INCLUDE_PATH");
  1779.     break;
  1780.       case 3:
  1781.     epath = getenv ("OBJCPLUS_INCLUDE_PATH");
  1782.     break;
  1783.       }
  1784.     /* If the environment var for this language is set,
  1785.        add to the default list of include directories.  */
  1786.     if (epath) {
  1787.       char *nstore = (char *) alloca (strlen (epath) + 2);
  1788.       int num_dirs;
  1789.       char *startp, *endp;
  1790.  
  1791.       for (num_dirs = 1, startp = epath; *startp; startp++)
  1792.     if (*startp == PATH_SEPARATOR)
  1793.       num_dirs++;
  1794.       include_defaults
  1795.     = (struct default_include *) xmalloc ((num_dirs
  1796.                            * sizeof (struct default_include))
  1797.                           + sizeof (include_defaults_array));
  1798.       startp = endp = epath;
  1799.       num_dirs = 0;
  1800.       while (1) {
  1801.         /* Handle cases like c:/usr/lib:d:/gcc/lib */
  1802.         if ((*endp == PATH_SEPARATOR
  1803. #if 0 /* Obsolete, now that we use semicolons as the path separator.  */
  1804. #ifdef __MSDOS__
  1805.          && (endp-startp != 1 || !isalpha (*startp))
  1806. #endif
  1807. #endif
  1808.          )
  1809.             || *endp == 0) {
  1810.       strncpy (nstore, startp, endp-startp);
  1811.       if (endp == startp)
  1812.         strcpy (nstore, ".");
  1813.       else
  1814.         nstore[endp-startp] = '\0';
  1815.  
  1816.       include_defaults[num_dirs].fname = savestring (nstore);
  1817.       include_defaults[num_dirs].cplusplus = cplusplus;
  1818.       include_defaults[num_dirs].cxx_aware = 1;
  1819.       num_dirs++;
  1820.       if (*endp == '\0')
  1821.         break;
  1822.       endp = startp = endp + 1;
  1823.     } else
  1824.       endp++;
  1825.       }
  1826.       /* Put the usual defaults back in at the end.  */
  1827.       bcopy ((char *) include_defaults_array,
  1828.          (char *) &include_defaults[num_dirs],
  1829.          sizeof (include_defaults_array));
  1830.     }
  1831.   }
  1832.  
  1833.   append_include_chain (before_system, last_before_system);
  1834.   first_system_include = before_system;
  1835.  
  1836.   /* Unless -fnostdinc,
  1837.      tack on the standard include file dirs to the specified list */
  1838.   if (!no_standard_includes) {
  1839.     struct default_include *p = include_defaults;
  1840.     char *specd_prefix = include_prefix;
  1841.     char *default_prefix = savestring (GCC_INCLUDE_DIR);
  1842.     int default_len = 0;
  1843.     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1844.     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
  1845.       default_len = strlen (default_prefix) - 7;
  1846.       default_prefix[default_len] = 0;
  1847.     }
  1848.     /* Search "translated" versions of GNU directories.
  1849.        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
  1850.     if (specd_prefix != 0 && default_len != 0)
  1851.       for (p = include_defaults; p->fname; p++) {
  1852.     /* Some standard dirs are only for C++.  */
  1853.     if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  1854.       /* Does this dir start with the prefix?  */
  1855.       if (!strncmp (p->fname, default_prefix, default_len)) {
  1856.         /* Yes; change prefix and add to search list.  */
  1857.         struct file_name_list *new
  1858.           = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  1859.         int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
  1860.         char *str = (char *) xmalloc (this_len + 1);
  1861.         strcpy (str, specd_prefix);
  1862.         strcat (str, p->fname + default_len);
  1863.         new->fname = str;
  1864.         new->control_macro = 0;
  1865.         new->c_system_include_path = !p->cxx_aware;
  1866.         new->got_name_map = 0;
  1867.         append_include_chain (new, new);
  1868.         if (first_system_include == 0)
  1869.           first_system_include = new;
  1870.       }
  1871.     }
  1872.       }
  1873.     /* Search ordinary names for GNU include directories.  */
  1874.     for (p = include_defaults; p->fname; p++) {
  1875.       /* Some standard dirs are only for C++.  */
  1876.       if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  1877.     struct file_name_list *new
  1878.       = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  1879.     new->control_macro = 0;
  1880.     new->c_system_include_path = !p->cxx_aware;
  1881.     new->fname = p->fname;
  1882.     new->got_name_map = 0;
  1883.     append_include_chain (new, new);
  1884.     if (first_system_include == 0)
  1885.       first_system_include = new;
  1886.       }
  1887.     }
  1888.   }
  1889.  
  1890.   /* Tack the after_include chain at the end of the include chain.  */
  1891.   append_include_chain (after_include, last_after_include);
  1892.   if (first_system_include == 0)
  1893.     first_system_include = after_include;
  1894.  
  1895.   /* With -v, print the list of dirs to search.  */
  1896.   if (verbose) {
  1897.     struct file_name_list *p;
  1898.     fprintf (stderr, "#include \"...\" search starts here:\n");
  1899.     for (p = include; p; p = p->next) {
  1900.       if (p == first_bracket_include)
  1901.     fprintf (stderr, "#include <...> search starts here:\n");
  1902.       fprintf (stderr, " %s\n", p->fname);
  1903.     }
  1904.     fprintf (stderr, "End of search list.\n");
  1905.   }
  1906.  
  1907.   /* Scan the -imacros files before the main input.
  1908.      Much like #including them, but with no_output set
  1909.      so that only their macro definitions matter.  */
  1910.  
  1911.   no_output++; no_record_file++;
  1912.   for (i = 1; i < argc; i++)
  1913.     if (pend_files[i]) {
  1914.       int fd = open (pend_files[i], O_RDONLY, 0666);
  1915.       if (fd < 0) {
  1916.     perror_with_name (pend_files[i]);
  1917.     return FAILURE_EXIT_CODE;
  1918.       }
  1919.       finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
  1920.     }
  1921.   no_output--; no_record_file--;
  1922.  
  1923.   /* Copy the entire contents of the main input file into
  1924.      the stacked input buffer previously allocated for it.  */
  1925.  
  1926.   /* JF check for stdin */
  1927.   if (in_fname == NULL || *in_fname == 0) {
  1928.     in_fname = "";
  1929.     f = 0;
  1930.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  1931.     goto perror;
  1932.  
  1933.   /* -MG doesn't select the form of output and must be specified with one of
  1934.      -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
  1935.      inhibit compilation.  */
  1936.   if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
  1937.     fatal ("-MG must be specified with one of -M or -MM");
  1938.  
  1939.   /* Either of two environment variables can specify output of deps.
  1940.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  1941.      where OUTPUT_FILE is the file to write deps info to
  1942.      and DEPS_TARGET is the target to mention in the deps.  */
  1943.  
  1944.   if (print_deps == 0
  1945.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  1946.       || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
  1947.     char *spec = getenv ("DEPENDENCIES_OUTPUT");
  1948.     char *s;
  1949.     char *output_file;
  1950.  
  1951.     if (spec == 0) {
  1952.       spec = getenv ("SUNPRO_DEPENDENCIES");
  1953.       print_deps = 2;
  1954.     }
  1955.     else
  1956.       print_deps = 1;
  1957.  
  1958.     s = spec;
  1959.     /* Find the space before the DEPS_TARGET, if there is one.  */
  1960.     /* This should use index.  (mrs) */
  1961.     while (*s != 0 && *s != ' ') s++;
  1962.     if (*s != 0) {
  1963.       deps_target = s + 1;
  1964.       output_file = (char *) xmalloc (s - spec + 1);
  1965.       bcopy (spec, output_file, s - spec);
  1966.       output_file[s - spec] = 0;
  1967.     }
  1968.     else {
  1969.       deps_target = 0;
  1970.       output_file = spec;
  1971.     }
  1972.       
  1973.     deps_file = output_file;
  1974.     deps_mode = "a";
  1975.   }
  1976.  
  1977.   /* For -M, print the expected object file name
  1978.      as the target of this Make-rule.  */
  1979.   if (print_deps) {
  1980.     deps_allocated_size = 200;
  1981.     deps_buffer = (char *) xmalloc (deps_allocated_size);
  1982.     deps_buffer[0] = 0;
  1983.     deps_size = 0;
  1984.     deps_column = 0;
  1985.  
  1986.     if (deps_target) {
  1987.       deps_output (deps_target, ':');
  1988.     } else if (*in_fname == 0) {
  1989.       deps_output ("-", ':');
  1990.     } else {
  1991.       char *p, *q;
  1992.       int len;
  1993.  
  1994.       /* Discard all directory prefixes from filename.  */
  1995.       if ((q = rindex (in_fname, '/')) != NULL
  1996. #ifdef DIR_SEPARATOR
  1997.       && (q = rindex (in_fname, DIR_SEPARATOR)) != NULL
  1998. #endif
  1999.       )
  2000.     ++q;
  2001.       else
  2002.     q = in_fname;
  2003.  
  2004.       /* Copy remainder to mungable area.  */
  2005.       p = (char *) alloca (strlen(q) + 8);
  2006.       strcpy (p, q);
  2007.  
  2008.       /* Output P, but remove known suffixes.  */
  2009.       len = strlen (p);
  2010.       q = p + len;
  2011.       if (len >= 2
  2012.       && p[len - 2] == '.'
  2013.       && index("cCsSm", p[len - 1]))
  2014.     q = p + (len - 2);
  2015.       else if (len >= 3
  2016.            && p[len - 3] == '.'
  2017.            && p[len - 2] == 'c'
  2018.            && p[len - 1] == 'c')
  2019.     q = p + (len - 3);
  2020.       else if (len >= 4
  2021.            && p[len - 4] == '.'
  2022.            && p[len - 3] == 'c'
  2023.            && p[len - 2] == 'x'
  2024.            && p[len - 1] == 'x')
  2025.     q = p + (len - 4);
  2026.       else if (len >= 4
  2027.            && p[len - 4] == '.'
  2028.            && p[len - 3] == 'c'
  2029.            && p[len - 2] == 'p'
  2030.            && p[len - 1] == 'p')
  2031.     q = p + (len - 4);
  2032.  
  2033.       /* Supply our own suffix.  */
  2034. #ifndef VMS
  2035.       strcpy (q, ".o");
  2036. #else
  2037.       strcpy (q, ".obj");
  2038. #endif
  2039.  
  2040.       deps_output (p, ':');
  2041.       deps_output (in_fname, ' ');
  2042.     }
  2043.   }
  2044.  
  2045.   file_size_and_mode (f, &st_mode, &st_size);
  2046.   fp->nominal_fname = fp->fname = in_fname;
  2047.   fp->lineno = 1;
  2048.   fp->system_header_p = 0;
  2049.   /* JF all this is mine about reading pipes and ttys */
  2050.   if (! S_ISREG (st_mode)) {
  2051.     /* Read input from a file that is not a normal disk file.
  2052.        We cannot preallocate a buffer with the correct size,
  2053.        so we must read in the file a piece at the time and make it bigger.  */
  2054.     int size;
  2055.     int bsize;
  2056.     int cnt;
  2057.  
  2058.     bsize = 2000;
  2059.     size = 0;
  2060.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  2061.     for (;;) {
  2062.       cnt = safe_read (f, fp->buf + size, bsize - size);
  2063.       if (cnt < 0) goto perror;    /* error! */
  2064.       size += cnt;
  2065.       if (size != bsize) break;    /* End of file */
  2066.       bsize *= 2;
  2067.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  2068.     }
  2069.     fp->length = size;
  2070.   } else {
  2071.     /* Read a file whose size we can determine in advance.
  2072.        For the sake of VMS, st_size is just an upper bound.  */
  2073.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  2074.     fp->length = safe_read (f, fp->buf, st_size);
  2075.     if (fp->length < 0) goto perror;
  2076.   }
  2077.   fp->bufp = fp->buf;
  2078.   fp->if_stack = if_stack;
  2079.  
  2080.   /* Make sure data ends with a newline.  And put a null after it.  */
  2081.  
  2082.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  2083.       /* Backslash-newline at end is not good enough.  */
  2084.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  2085.     fp->buf[fp->length++] = '\n';
  2086.     missing_newline = 1;
  2087.   }
  2088.   fp->buf[fp->length] = '\0';
  2089.  
  2090.   /* Unless inhibited, convert trigraphs in the input.  */
  2091.  
  2092.   if (!no_trigraphs)
  2093.     trigraph_pcp (fp);
  2094.  
  2095.   /* Now that we know the input file is valid, open the output.  */
  2096.  
  2097.   if (!out_fname || !strcmp (out_fname, ""))
  2098.     out_fname = "stdout";
  2099.   else if (! freopen (out_fname, "w", stdout))
  2100.     pfatal_with_name (out_fname);
  2101.  
  2102.   output_line_command (fp, &outbuf, 0, same_file);
  2103.  
  2104.   /* Scan the -include files before the main input.  */
  2105.  
  2106.   no_record_file++;
  2107.   for (i = 1; i < argc; i++)
  2108.     if (pend_includes[i]) {
  2109.       int fd = open (pend_includes[i], O_RDONLY, 0666);
  2110.       if (fd < 0) {
  2111.     perror_with_name (pend_includes[i]);
  2112.     return FAILURE_EXIT_CODE;
  2113.       }
  2114.       finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
  2115.     }
  2116.   no_record_file--;
  2117.  
  2118.   /* Scan the input, processing macros and directives.  */
  2119.  
  2120.   rescan (&outbuf, 0);
  2121.  
  2122.   if (missing_newline)
  2123.     fp->lineno--;
  2124.  
  2125.   if (pedantic && missing_newline)
  2126.     pedwarn ("file does not end in newline");
  2127.  
  2128.   /* Now we have processed the entire input
  2129.      Write whichever kind of output has been requested.  */
  2130.  
  2131.   if (dump_macros == dump_only)
  2132.     dump_all_macros ();
  2133.   else if (! inhibit_output) {
  2134.     write_output ();
  2135.   }
  2136.  
  2137.   if (print_deps) {
  2138.     /* Don't actually write the deps file if compilation has failed.  */
  2139.     if (errors == 0) {
  2140.       if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
  2141.     pfatal_with_name (deps_file);
  2142.       fputs (deps_buffer, deps_stream);
  2143.       putc ('\n', deps_stream);
  2144.       if (deps_file) {
  2145.     if (ferror (deps_stream) || fclose (deps_stream) != 0)
  2146.       fatal ("I/O error on output");
  2147.       }
  2148.     }
  2149.   }
  2150.  
  2151.   if (pcp_outfile && pcp_outfile != stdout
  2152.       && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
  2153.     fatal ("I/O error on `-pcp' output");
  2154.  
  2155.   if (ferror (stdout) || fclose (stdout) != 0)
  2156.     fatal ("I/O error on output");
  2157.  
  2158.   if (errors)
  2159.     exit (FAILURE_EXIT_CODE);
  2160.   exit (SUCCESS_EXIT_CODE);
  2161.  
  2162.  perror:
  2163.   pfatal_with_name (in_fname);
  2164.   return 0;
  2165. }
  2166.  
  2167. /* Given a colon-separated list of file names PATH,
  2168.    add all the names to the search path for include files.  */
  2169.  
  2170. static void
  2171. path_include (path)
  2172.      char *path;
  2173. {
  2174.   char *p;
  2175.  
  2176.   p = path;
  2177.  
  2178.   if (*p)
  2179.     while (1) {
  2180.       char *q = p;
  2181.       char *name;
  2182.       struct file_name_list *dirtmp;
  2183.  
  2184.       /* Find the end of this name.  */
  2185.       while (*q != 0 && *q != PATH_SEPARATOR) q++;
  2186.       if (p == q) {
  2187.     /* An empty name in the path stands for the current directory.  */
  2188.     name = (char *) xmalloc (2);
  2189.     name[0] = '.';
  2190.     name[1] = 0;
  2191.       } else {
  2192.     /* Otherwise use the directory that is named.  */
  2193.     name = (char *) xmalloc (q - p + 1);
  2194.     bcopy (p, name, q - p);
  2195.     name[q - p] = 0;
  2196.       }
  2197.  
  2198.       dirtmp = (struct file_name_list *)
  2199.     xmalloc (sizeof (struct file_name_list));
  2200.       dirtmp->next = 0;        /* New one goes on the end */
  2201.       dirtmp->control_macro = 0;
  2202.       dirtmp->c_system_include_path = 0;
  2203.       dirtmp->fname = name;
  2204.       dirtmp->got_name_map = 0;
  2205.       append_include_chain (dirtmp, dirtmp);
  2206.  
  2207.       /* Advance past this name.  */
  2208.       p = q;
  2209.       if (*p == 0)
  2210.     break;
  2211.       /* Skip the colon.  */
  2212.       p++;
  2213.     }
  2214. }
  2215.  
  2216. /* Return the address of the first character in S that equals C.
  2217.    S is an array of length N, possibly containing '\0's, and followed by '\0'.
  2218.    Return 0 if there is no such character.  Assume that C itself is not '\0'.
  2219.    If we knew we could use memchr, we could just invoke memchr (S, C, N),
  2220.    but unfortunately memchr isn't autoconfigured yet.  */
  2221.  
  2222. static U_CHAR *
  2223. index0 (s, c, n)
  2224.      U_CHAR *s;
  2225.      int c;
  2226.      int n;
  2227. {
  2228.   for (;;) {
  2229.     char *q = index (s, c);
  2230.     if (q)
  2231.       return (U_CHAR *) q;
  2232.     else {
  2233.       int l = strlen (s);
  2234.       if (l == n)
  2235.     return 0;
  2236.       l++;
  2237.       s += l;
  2238.       n -= l;
  2239.     }
  2240.   }
  2241. }
  2242.  
  2243. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  2244.    before main CCCP processing.  Name `pcp' is also in honor of the
  2245.    drugs the trigraph designers must have been on.
  2246.  
  2247.    Using an extra pass through the buffer takes a little extra time,
  2248.    but is infinitely less hairy than trying to handle trigraphs inside
  2249.    strings, etc. everywhere, and also makes sure that trigraphs are
  2250.    only translated in the top level of processing. */
  2251.  
  2252. static void
  2253. trigraph_pcp (buf)
  2254.      FILE_BUF *buf;
  2255. {
  2256.   register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
  2257.   int len;
  2258.  
  2259.   fptr = bptr = sptr = buf->buf;
  2260.   lptr = fptr + buf->length;
  2261.   while ((sptr = (U_CHAR *) index0 (sptr, '?', lptr - sptr)) != NULL) {
  2262.     if (*++sptr != '?')
  2263.       continue;
  2264.     switch (*++sptr) {
  2265.       case '=':
  2266.       c = '#';
  2267.       break;
  2268.     case '(':
  2269.       c = '[';
  2270.       break;
  2271.     case '/':
  2272.       c = '\\';
  2273.       break;
  2274.     case ')':
  2275.       c = ']';
  2276.       break;
  2277.     case '\'':
  2278.       c = '^';
  2279.       break;
  2280.     case '<':
  2281.       c = '{';
  2282.       break;
  2283.     case '!':
  2284.       c = '|';
  2285.       break;
  2286.     case '>':
  2287.       c = '}';
  2288.       break;
  2289.     case '-':
  2290.       c  = '~';
  2291.       break;
  2292.     case '?':
  2293.       sptr--;
  2294.       continue;
  2295.     default:
  2296.       continue;
  2297.     }
  2298.     len = sptr - fptr - 2;
  2299.  
  2300.     /* BSD doc says bcopy () works right for overlapping strings.  In ANSI
  2301.        C, this will be memmove (). */
  2302.     if (bptr != fptr && len > 0)
  2303.       bcopy ((char *) fptr, (char *) bptr, len);
  2304.  
  2305.     bptr += len;
  2306.     *bptr++ = c;
  2307.     fptr = ++sptr;
  2308.   }
  2309.   len = buf->length - (fptr - buf->buf);
  2310.   if (bptr != fptr && len > 0)
  2311.     bcopy ((char *) fptr, (char *) bptr, len);
  2312.   buf->length -= fptr - bptr;
  2313.   buf->buf[buf->length] = '\0';
  2314.   if (warn_trigraphs && fptr != bptr)
  2315.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  2316. }
  2317.  
  2318. /* Move all backslash-newline pairs out of embarrassing places.
  2319.    Exchange all such pairs following BP
  2320.    with any potentially-embarrassing characters that follow them.
  2321.    Potentially-embarrassing characters are / and *
  2322.    (because a backslash-newline inside a comment delimiter
  2323.    would cause it not to be recognized).  */
  2324.  
  2325. static void
  2326. newline_fix (bp)
  2327.      U_CHAR *bp;
  2328. {
  2329.   register U_CHAR *p = bp;
  2330.  
  2331.   /* First count the backslash-newline pairs here.  */
  2332.  
  2333.   while (p[0] == '\\' && p[1] == '\n')
  2334.     p += 2;
  2335.  
  2336.   /* What follows the backslash-newlines is not embarrassing.  */
  2337.  
  2338.   if (*p != '/' && *p != '*')
  2339.     return;
  2340.  
  2341.   /* Copy all potentially embarrassing characters
  2342.      that follow the backslash-newline pairs
  2343.      down to where the pairs originally started.  */
  2344.  
  2345.   while (*p == '*' || *p == '/')
  2346.     *bp++ = *p++;
  2347.  
  2348.   /* Now write the same number of pairs after the embarrassing chars.  */
  2349.   while (bp < p) {
  2350.     *bp++ = '\\';
  2351.     *bp++ = '\n';
  2352.   }
  2353. }
  2354.  
  2355. /* Like newline_fix but for use within a directive-name.
  2356.    Move any backslash-newlines up past any following symbol constituents.  */
  2357.  
  2358. static void
  2359. name_newline_fix (bp)
  2360.      U_CHAR *bp;
  2361. {
  2362.   register U_CHAR *p = bp;
  2363.  
  2364.   /* First count the backslash-newline pairs here.  */
  2365.   while (p[0] == '\\' && p[1] == '\n')
  2366.     p += 2;
  2367.  
  2368.   /* What follows the backslash-newlines is not embarrassing.  */
  2369.  
  2370.   if (!is_idchar[*p])
  2371.     return;
  2372.  
  2373.   /* Copy all potentially embarrassing characters
  2374.      that follow the backslash-newline pairs
  2375.      down to where the pairs originally started.  */
  2376.  
  2377.   while (is_idchar[*p])
  2378.     *bp++ = *p++;
  2379.  
  2380.   /* Now write the same number of pairs after the embarrassing chars.  */
  2381.   while (bp < p) {
  2382.     *bp++ = '\\';
  2383.     *bp++ = '\n';
  2384.   }
  2385. }
  2386.  
  2387. /* Look for lint commands in comments.
  2388.  
  2389.    When we come in here, ibp points into a comment.  Limit is as one expects.
  2390.    scan within the comment -- it should start, after lwsp, with a lint command.
  2391.    If so that command is returned as a (constant) string.
  2392.  
  2393.    Upon return, any arg will be pointed to with argstart and will be
  2394.    arglen long.  Note that we don't parse that arg since it will just
  2395.    be printed out again.
  2396. */
  2397.  
  2398. static char *
  2399. get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
  2400.      register U_CHAR *ibp;
  2401.      register U_CHAR *limit;
  2402.      U_CHAR **argstart;        /* point to command arg */
  2403.      int *arglen, *cmdlen;    /* how long they are */
  2404. {
  2405.   long linsize;
  2406.   register U_CHAR *numptr;    /* temp for arg parsing */
  2407.  
  2408.   *arglen = 0;
  2409.  
  2410.   SKIP_WHITE_SPACE (ibp);
  2411.  
  2412.   if (ibp >= limit) return NULL;
  2413.  
  2414.   linsize = limit - ibp;
  2415.   
  2416.   /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
  2417.   if ((linsize >= 10) && !strncmp (ibp, "NOTREACHED", 10)) {
  2418.     *cmdlen = 10;
  2419.     return "NOTREACHED";
  2420.   }
  2421.   if ((linsize >= 8) && !strncmp (ibp, "ARGSUSED", 8)) {
  2422.     *cmdlen = 8;
  2423.     return "ARGSUSED";
  2424.   }
  2425.   if ((linsize >= 11) && !strncmp (ibp, "LINTLIBRARY", 11)) {
  2426.     *cmdlen = 11;
  2427.     return "LINTLIBRARY";
  2428.   }
  2429.   if ((linsize >= 7) && !strncmp (ibp, "VARARGS", 7)) {
  2430.     *cmdlen = 7;
  2431.     ibp += 7; linsize -= 7;
  2432.     if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
  2433.  
  2434.     /* OK, read a number */
  2435.     for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
  2436.      numptr++);
  2437.     *arglen = numptr - *argstart;
  2438.     return "VARARGS";
  2439.   }
  2440.   return NULL;
  2441. }
  2442.  
  2443. /*
  2444.  * The main loop of the program.
  2445.  *
  2446.  * Read characters from the input stack, transferring them to the
  2447.  * output buffer OP.
  2448.  *
  2449.  * Macros are expanded and push levels on the input stack.
  2450.  * At the end of such a level it is popped off and we keep reading.
  2451.  * At the end of any other kind of level, we return.
  2452.  * #-directives are handled, except within macros.
  2453.  *
  2454.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  2455.  * and insert them when appropriate.  This is set while scanning macro
  2456.  * arguments before substitution.  It is zero when scanning for final output.
  2457.  *   There are three types of Newline markers:
  2458.  *   * Newline -  follows a macro name that was not expanded
  2459.  *     because it appeared inside an expansion of the same macro.
  2460.  *     This marker prevents future expansion of that identifier.
  2461.  *     When the input is rescanned into the final output, these are deleted.
  2462.  *     These are also deleted by ## concatenation.
  2463.  *   * Newline Space (or Newline and any other whitespace character)
  2464.  *     stands for a place that tokens must be separated or whitespace
  2465.  *     is otherwise desirable, but where the ANSI standard specifies there
  2466.  *     is no whitespace.  This marker turns into a Space (or whichever other
  2467.  *     whitespace char appears in the marker) in the final output,
  2468.  *     but it turns into nothing in an argument that is stringified with #.
  2469.  *     Such stringified arguments are the only place where the ANSI standard
  2470.  *     specifies with precision that whitespace may not appear.
  2471.  *
  2472.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  2473.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  2474.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  2475.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  2476.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  2477.  */
  2478.  
  2479. static void
  2480. rescan (op, output_marks)
  2481.      FILE_BUF *op;
  2482.      int output_marks;
  2483. {
  2484.   /* Character being scanned in main loop.  */
  2485.   register U_CHAR c;
  2486.  
  2487.   /* Length of pending accumulated identifier.  */
  2488.   register int ident_length = 0;
  2489.  
  2490.   /* Hash code of pending accumulated identifier.  */
  2491.   register int hash = 0;
  2492.  
  2493.   /* Current input level (&instack[indepth]).  */
  2494.   FILE_BUF *ip;
  2495.  
  2496.   /* Pointer for scanning input.  */
  2497.   register U_CHAR *ibp;
  2498.  
  2499.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  2500.   register U_CHAR *limit;
  2501.  
  2502.   /* Pointer for storing output.  */
  2503.   register U_CHAR *obp;
  2504.  
  2505.   /* REDO_CHAR is nonzero if we are processing an identifier
  2506.      after backing up over the terminating character.
  2507.      Sometimes we process an identifier without backing up over
  2508.      the terminating character, if the terminating character
  2509.      is not special.  Backing up is done so that the terminating character
  2510.      will be dispatched on again once the identifier is dealt with.  */
  2511.   int redo_char = 0;
  2512.  
  2513.   /* 1 if within an identifier inside of which a concatenation
  2514.      marker (Newline -) has been seen.  */
  2515.   int concatenated = 0;
  2516.  
  2517.   /* While scanning a comment or a string constant,
  2518.      this records the line it started on, for error messages.  */
  2519.   int start_line;
  2520.  
  2521.   /* Record position of last `real' newline.  */
  2522.   U_CHAR *beg_of_line;
  2523.  
  2524. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  2525.  
  2526. #define POPMACRO \
  2527. do { ip->macro->type = T_MACRO;        \
  2528.      if (ip->free_ptr) free (ip->free_ptr);    \
  2529.      --indepth; } while (0)
  2530.  
  2531. /* Reload `rescan's local variables that describe the current
  2532.    level of the input stack.  */
  2533.  
  2534. #define RECACHE  \
  2535. do { ip = &instack[indepth];        \
  2536.      ibp = ip->bufp;            \
  2537.      limit = ip->buf + ip->length;    \
  2538.      op->bufp = obp;            \
  2539.      check_expand (op, limit - ibp);    \
  2540.      beg_of_line = 0;            \
  2541.      obp = op->bufp; } while (0)
  2542.  
  2543.   if (no_output && instack[indepth].fname != 0)
  2544.     skip_if_group (&instack[indepth], 1, NULL);
  2545.  
  2546.   obp = op->bufp;
  2547.   RECACHE;
  2548.  
  2549.   beg_of_line = ibp;
  2550.  
  2551.   /* Our caller must always put a null after the end of
  2552.      the input at each input stack level.  */
  2553.   if (*limit != 0)
  2554.     abort ();
  2555.  
  2556.   while (1) {
  2557.     c = *ibp++;
  2558.     *obp++ = c;
  2559.  
  2560.     switch (c) {
  2561.     case '\\':
  2562.       if (*ibp == '\n' && !ip->macro) {
  2563.     /* At the top level, always merge lines ending with backslash-newline,
  2564.        even in middle of identifier.  But do not merge lines in a macro,
  2565.        since backslash might be followed by a newline-space marker.  */
  2566.     ++ibp;
  2567.     ++ip->lineno;
  2568.     --obp;        /* remove backslash from obuf */
  2569.     break;
  2570.       }
  2571.       /* If ANSI, backslash is just another character outside a string.  */
  2572.       if (!traditional)
  2573.     goto randomchar;
  2574.       /* Otherwise, backslash suppresses specialness of following char,
  2575.      so copy it here to prevent the switch from seeing it.
  2576.      But first get any pending identifier processed.  */
  2577.       if (ident_length > 0)
  2578.     goto specialchar;
  2579.       if (ibp < limit)
  2580.     *obp++ = *ibp++;
  2581.       break;
  2582.  
  2583.     case '#':
  2584.       if (assertions_flag) {
  2585.     /* Copy #foo (bar lose) without macro expansion.  */
  2586.     SKIP_WHITE_SPACE (ibp);
  2587.     while (is_idchar[*ibp])
  2588.       *obp++ = *ibp++;
  2589.     SKIP_WHITE_SPACE (ibp);
  2590.     if (*ibp == '(') {
  2591.       ip->bufp = ibp;
  2592.       skip_paren_group (ip);
  2593.       bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
  2594.       obp += ip->bufp - ibp;
  2595.       ibp = ip->bufp;
  2596.     }
  2597.       }
  2598.  
  2599.       /* If this is expanding a macro definition, don't recognize
  2600.      preprocessor directives.  */
  2601.       if (ip->macro != 0)
  2602.     goto randomchar;
  2603.       /* If this is expand_into_temp_buffer, recognize them
  2604.      only after an actual newline at this level,
  2605.      not at the beginning of the input level.  */
  2606.       if (ip->fname == 0 && beg_of_line == ip->buf)
  2607.     goto randomchar;
  2608.       if (ident_length)
  2609.     goto specialchar;
  2610.  
  2611.       
  2612.       /* # keyword: a # must be first nonblank char on the line */
  2613.       if (beg_of_line == 0)
  2614.     goto randomchar;
  2615.       {
  2616.     U_CHAR *bp;
  2617.  
  2618.     /* Scan from start of line, skipping whitespace, comments
  2619.        and backslash-newlines, and see if we reach this #.
  2620.        If not, this # is not special.  */
  2621.     bp = beg_of_line;
  2622.     /* If -traditional, require # to be at beginning of line.  */
  2623.     if (!traditional)
  2624.       while (1) {
  2625.         if (is_hor_space[*bp])
  2626.           bp++;
  2627.         else if (*bp == '\\' && bp[1] == '\n')
  2628.           bp += 2;
  2629.         else if (*bp == '/' && bp[1] == '*') {
  2630.           bp += 2;
  2631.           while (!(*bp == '*' && bp[1] == '/'))
  2632.         bp++;
  2633.           bp += 2;
  2634.         }
  2635.         /* There is no point in trying to deal with C++ // comments here,
  2636.            because if there is one, then this # must be part of the
  2637.            comment and we would never reach here.  */
  2638.         else break;
  2639.       }
  2640.     if (bp + 1 != ibp)
  2641.       goto randomchar;
  2642.       }
  2643.  
  2644.       /* This # can start a directive.  */
  2645.  
  2646.       --obp;        /* Don't copy the '#' */
  2647.  
  2648.       ip->bufp = ibp;
  2649.       op->bufp = obp;
  2650.       if (! handle_directive (ip, op)) {
  2651. #ifdef USE_C_ALLOCA
  2652.     alloca (0);
  2653. #endif
  2654.     /* Not a known directive: treat it as ordinary text.
  2655.        IP, OP, IBP, etc. have not been changed.  */
  2656.     if (no_output && instack[indepth].fname) {
  2657.       /* If not generating expanded output,
  2658.          what we do with ordinary text is skip it.
  2659.          Discard everything until next # directive.  */
  2660.       skip_if_group (&instack[indepth], 1, 0);
  2661.       RECACHE;
  2662.       beg_of_line = ibp;
  2663.       break;
  2664.     }
  2665.     ++obp;        /* Copy the '#' after all */
  2666.     /* Don't expand an identifier that could be a macro directive.
  2667.        (Section 3.8.3 of the ANSI C standard)            */
  2668.     SKIP_WHITE_SPACE (ibp);
  2669.     if (is_idstart[*ibp])
  2670.       {
  2671.         *obp++ = *ibp++;
  2672.         while (is_idchar[*ibp])
  2673.           *obp++ = *ibp++;
  2674.       }
  2675.     goto randomchar;
  2676.       }
  2677. #ifdef USE_C_ALLOCA
  2678.       alloca (0);
  2679. #endif
  2680.       /* A # directive has been successfully processed.  */
  2681.       /* If not generating expanded output, ignore everything until
  2682.      next # directive.  */
  2683.       if (no_output && instack[indepth].fname)
  2684.     skip_if_group (&instack[indepth], 1, 0);
  2685.       obp = op->bufp;
  2686.       RECACHE;
  2687.       beg_of_line = ibp;
  2688.       break;
  2689.  
  2690.     case '\"':            /* skip quoted string */
  2691.     case '\'':
  2692.       /* A single quoted string is treated like a double -- some
  2693.      programs (e.g., troff) are perverse this way */
  2694.  
  2695.       if (ident_length)
  2696.     goto specialchar;
  2697.  
  2698.       start_line = ip->lineno;
  2699.  
  2700.       /* Skip ahead to a matching quote.  */
  2701.  
  2702.       while (1) {
  2703.     if (ibp >= limit) {
  2704.       if (ip->macro != 0) {
  2705.         /* try harder: this string crosses a macro expansion boundary.
  2706.            This can happen naturally if -traditional.
  2707.            Otherwise, only -D can make a macro with an unmatched quote.  */
  2708.         POPMACRO;
  2709.         RECACHE;
  2710.         continue;
  2711.       }
  2712.       if (!traditional) {
  2713.         error_with_line (line_for_error (start_line),
  2714.                  "unterminated string or character constant");
  2715.         error_with_line (multiline_string_line,
  2716.                  "possible real start of unterminated constant");
  2717.         multiline_string_line = 0;
  2718.       }
  2719.       break;
  2720.     }
  2721.     *obp++ = *ibp;
  2722.     switch (*ibp++) {
  2723.     case '\n':
  2724.       ++ip->lineno;
  2725.       ++op->lineno;
  2726.       /* Traditionally, end of line ends a string constant with no error.
  2727.          So exit the loop and record the new line.  */
  2728.       if (traditional) {
  2729.         beg_of_line = ibp;
  2730.         goto while2end;
  2731.       }
  2732.       if (c == '\'') {
  2733.         error_with_line (line_for_error (start_line),
  2734.                  "unterminated character constant");
  2735.         goto while2end;
  2736.       }
  2737.       if (pedantic && multiline_string_line == 0) {
  2738.         pedwarn_with_line (line_for_error (start_line),
  2739.                    "string constant runs past end of line");
  2740.       }
  2741.       if (multiline_string_line == 0)
  2742.         multiline_string_line = ip->lineno - 1;
  2743.       break;
  2744.  
  2745.     case '\\':
  2746.       if (ibp >= limit)
  2747.         break;
  2748.       if (*ibp == '\n') {
  2749.         /* Backslash newline is replaced by nothing at all,
  2750.            but keep the line counts correct.  */
  2751.         --obp;
  2752.         ++ibp;
  2753.         ++ip->lineno;
  2754.       } else {
  2755.         /* ANSI stupidly requires that in \\ the second \
  2756.            is *not* prevented from combining with a newline.  */
  2757.         while (*ibp == '\\' && ibp[1] == '\n') {
  2758.           ibp += 2;
  2759.           ++ip->lineno;
  2760.         }
  2761.         *obp++ = *ibp++;
  2762.       }
  2763.       break;
  2764.  
  2765.     case '\"':
  2766.     case '\'':
  2767.       if (ibp[-1] == c)
  2768.         goto while2end;
  2769.       break;
  2770.     }
  2771.       }
  2772.     while2end:
  2773.       break;
  2774.  
  2775.     case '/':
  2776.       if (*ibp == '\\' && ibp[1] == '\n')
  2777.     newline_fix (ibp);
  2778.  
  2779.       if (*ibp != '*'
  2780.       && !(cplusplus_comments && *ibp == '/'))
  2781.     goto randomchar;
  2782.       if (ip->macro != 0)
  2783.     goto randomchar;
  2784.       if (ident_length)
  2785.     goto specialchar;
  2786.  
  2787.       if (*ibp == '/') {
  2788.     /* C++ style comment... */
  2789.     start_line = ip->lineno;
  2790.  
  2791.     --ibp;            /* Back over the slash */
  2792.     --obp;
  2793.  
  2794.     /* Comments are equivalent to spaces. */
  2795.     if (! put_out_comments)
  2796.       *obp++ = ' ';
  2797.     else {
  2798.       /* must fake up a comment here */
  2799.       *obp++ = '/';
  2800.       *obp++ = '/';
  2801.     }
  2802.     {
  2803.       U_CHAR *before_bp = ibp+2;
  2804.  
  2805.       while (ibp < limit) {
  2806.         if (ibp[-1] != '\\' && *ibp == '\n') {
  2807.           if (put_out_comments) {
  2808.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  2809.         obp += ibp - before_bp;
  2810.           }
  2811.           break;
  2812.         } else {
  2813.           if (*ibp == '\n') {
  2814.         ++ip->lineno;
  2815.         /* Copy the newline into the output buffer, in order to
  2816.            avoid the pain of a #line every time a multiline comment
  2817.            is seen.  */
  2818.         if (!put_out_comments)
  2819.           *obp++ = '\n';
  2820.         ++op->lineno;
  2821.           }
  2822.           ibp++;
  2823.         }
  2824.       }
  2825.       break;
  2826.     }
  2827.       }
  2828.  
  2829.       /* Ordinary C comment.  Skip it, optionally copying it to output.  */
  2830.  
  2831.       start_line = ip->lineno;
  2832.  
  2833.       ++ibp;            /* Skip the star. */
  2834.  
  2835.       /* If this cpp is for lint, we peek inside the comments: */
  2836.       if (for_lint) {
  2837.     U_CHAR *argbp;
  2838.     int cmdlen, arglen;
  2839.     char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
  2840.  
  2841.     if (lintcmd != NULL) {
  2842.       op->bufp = obp;
  2843.       check_expand (op, cmdlen + arglen + 14);
  2844.       obp = op->bufp;
  2845.       /* I believe it is always safe to emit this newline: */
  2846.       obp[-1] = '\n';
  2847.       bcopy ("#pragma lint ", (char *) obp, 13);
  2848.       obp += 13;
  2849.       bcopy (lintcmd, (char *) obp, cmdlen);
  2850.       obp += cmdlen;
  2851.  
  2852.       if (arglen != 0) {
  2853.         *(obp++) = ' ';
  2854.         bcopy (argbp, (char *) obp, arglen);
  2855.         obp += arglen;
  2856.       }
  2857.  
  2858.       /* OK, now bring us back to the state we were in before we entered
  2859.          this branch.  We need #line because the #pragma's newline always
  2860.          messes up the line count.  */
  2861.       op->bufp = obp;
  2862.       output_line_command (ip, op, 0, same_file);
  2863.       check_expand (op, limit - ibp + 2);
  2864.       obp = op->bufp;
  2865.       *(obp++) = '/';
  2866.     }
  2867.       }
  2868.  
  2869.       /* Comments are equivalent to spaces.
  2870.      Note that we already output the slash; we might not want it.
  2871.      For -traditional, a comment is equivalent to nothing.  */
  2872.       if (! put_out_comments) {
  2873.     if (traditional)
  2874.       obp--;
  2875.     else
  2876.       obp[-1] = ' ';
  2877.       }
  2878.       else
  2879.     *obp++ = '*';
  2880.  
  2881.       {
  2882.     U_CHAR *before_bp = ibp;
  2883.  
  2884.     while (ibp < limit) {
  2885.       switch (*ibp++) {
  2886.       case '/':
  2887.         if (warn_comments && ibp < limit && *ibp == '*')
  2888.           warning ("`/*' within comment");
  2889.         break;
  2890.       case '*':
  2891.         if (*ibp == '\\' && ibp[1] == '\n')
  2892.           newline_fix (ibp);
  2893.         if (ibp >= limit || *ibp == '/')
  2894.           goto comment_end;
  2895.         break;
  2896.       case '\n':
  2897.         ++ip->lineno;
  2898.         /* Copy the newline into the output buffer, in order to
  2899.            avoid the pain of a #line every time a multiline comment
  2900.            is seen.  */
  2901.         if (!put_out_comments)
  2902.           *obp++ = '\n';
  2903.         ++op->lineno;
  2904.       }
  2905.     }
  2906.       comment_end:
  2907.  
  2908.     if (ibp >= limit)
  2909.       error_with_line (line_for_error (start_line),
  2910.                "unterminated comment");
  2911.     else {
  2912.       ibp++;
  2913.       if (put_out_comments) {
  2914.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  2915.         obp += ibp - before_bp;
  2916.       }
  2917.     }
  2918.       }
  2919.       break;
  2920.  
  2921.     case '$':
  2922.       if (!dollars_in_ident)
  2923.     goto randomchar;
  2924.       goto letter;
  2925.  
  2926.     case '0': case '1': case '2': case '3': case '4':
  2927.     case '5': case '6': case '7': case '8': case '9':
  2928.       /* If digit is not part of identifier, it starts a number,
  2929.      which means that following letters are not an identifier.
  2930.      "0x5" does not refer to an identifier "x5".
  2931.      So copy all alphanumerics that follow without accumulating
  2932.      as an identifier.  Periods also, for sake of "3.e7".  */
  2933.  
  2934.       if (ident_length == 0) {
  2935.     while (ibp < limit) {
  2936.       while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  2937.         ++ip->lineno;
  2938.         ibp += 2;
  2939.       }
  2940.       c = *ibp++;
  2941.       if (!is_idchar[c] && c != '.') {
  2942.         --ibp;
  2943.         break;
  2944.       }
  2945.       *obp++ = c;
  2946.       /* A sign can be part of a preprocessing number
  2947.          if it follows an e.  */
  2948.       if (c == 'e' || c == 'E') {
  2949.         while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  2950.           ++ip->lineno;
  2951.           ibp += 2;
  2952.         }
  2953.         if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
  2954.           *obp++ = *ibp++;
  2955.           /* But traditional C does not let the token go past the sign.  */
  2956.           if (traditional)
  2957.         break;
  2958.         }
  2959.       }
  2960.     }
  2961.     break;
  2962.       }
  2963.       /* fall through */
  2964.  
  2965.     case '_':
  2966.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  2967.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  2968.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  2969.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  2970.     case 'y': case 'z':
  2971.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  2972.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  2973.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  2974.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  2975.     case 'Y': case 'Z':
  2976.     letter:
  2977.       ident_length++;
  2978.       /* Compute step of hash function, to avoid a proc call on every token */
  2979.       hash = HASHSTEP (hash, c);
  2980.       break;
  2981.  
  2982.     case '\n':
  2983.       if (ip->fname == 0 && *ibp == '-') {
  2984.     /* Newline - inhibits expansion of preceding token.
  2985.        If expanding a macro arg, we keep the newline -.
  2986.        In final output, it is deleted.
  2987.        We recognize Newline - in macro bodies and macro args.  */
  2988.     if (! concatenated) {
  2989.       ident_length = 0;
  2990.       hash = 0;
  2991.     }
  2992.     ibp++;
  2993.     if (!output_marks) {
  2994.       obp--;
  2995.     } else {
  2996.       /* If expanding a macro arg, keep the newline -.  */
  2997.       *obp++ = '-';
  2998.     }
  2999.     break;
  3000.       }
  3001.  
  3002.       /* If reprocessing a macro expansion, newline is a special marker.  */
  3003.       else if (ip->macro != 0) {
  3004.     /* Newline White is a "funny space" to separate tokens that are
  3005.        supposed to be separate but without space between.
  3006.        Here White means any whitespace character.
  3007.        Newline - marks a recursive macro use that is not
  3008.        supposed to be expandable.  */
  3009.  
  3010.     if (is_space[*ibp]) {
  3011.       /* Newline Space does not prevent expansion of preceding token
  3012.          so expand the preceding token and then come back.  */
  3013.       if (ident_length > 0)
  3014.         goto specialchar;
  3015.  
  3016.       /* If generating final output, newline space makes a space.  */
  3017.       if (!output_marks) {
  3018.         obp[-1] = *ibp++;
  3019.         /* And Newline Newline makes a newline, so count it.  */
  3020.         if (obp[-1] == '\n')
  3021.           op->lineno++;
  3022.       } else {
  3023.         /* If expanding a macro arg, keep the newline space.
  3024.            If the arg gets stringified, newline space makes nothing.  */
  3025.         *obp++ = *ibp++;
  3026.       }
  3027.     } else abort ();    /* Newline followed by something random?  */
  3028.     break;
  3029.       }
  3030.  
  3031.       /* If there is a pending identifier, handle it and come back here.  */
  3032.       if (ident_length > 0)
  3033.     goto specialchar;
  3034.  
  3035.       beg_of_line = ibp;
  3036.  
  3037.       /* Update the line counts and output a #line if necessary.  */
  3038.       ++ip->lineno;
  3039.       ++op->lineno;
  3040.       if (ip->lineno != op->lineno) {
  3041.     op->bufp = obp;
  3042.     output_line_command (ip, op, 1, same_file);
  3043.     check_expand (op, limit - ibp);
  3044.     obp = op->bufp;
  3045.       }
  3046.       break;
  3047.  
  3048.       /* Come here either after (1) a null character that is part of the input
  3049.      or (2) at the end of the input, because there is a null there.  */
  3050.     case 0:
  3051.       if (ibp <= limit)
  3052.     /* Our input really contains a null character.  */
  3053.     goto randomchar;
  3054.  
  3055.       /* At end of a macro-expansion level, pop it and read next level.  */
  3056.       if (ip->macro != 0) {
  3057.     obp--;
  3058.     ibp--;
  3059.     /* If traditional, and we have an identifier that ends here,
  3060.        process it now, so we get the right error for recursion.  */
  3061.     if (traditional && ident_length
  3062.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  3063.       redo_char = 1;
  3064.       goto randomchar;
  3065.     }
  3066.     POPMACRO;
  3067.     RECACHE;
  3068.     break;
  3069.       }
  3070.  
  3071.       /* If we don't have a pending identifier,
  3072.      return at end of input.  */
  3073.       if (ident_length == 0) {
  3074.     obp--;
  3075.     ibp--;
  3076.     op->bufp = obp;
  3077.     ip->bufp = ibp;
  3078.     goto ending;
  3079.       }
  3080.  
  3081.       /* If we do have a pending identifier, just consider this null
  3082.      a special character and arrange to dispatch on it again.
  3083.      The second time, IDENT_LENGTH will be zero so we will return.  */
  3084.  
  3085.       /* Fall through */
  3086.  
  3087. specialchar:
  3088.  
  3089.       /* Handle the case of a character such as /, ', " or null
  3090.      seen following an identifier.  Back over it so that
  3091.      after the identifier is processed the special char
  3092.      will be dispatched on again.  */
  3093.  
  3094.       ibp--;
  3095.       obp--;
  3096.       redo_char = 1;
  3097.  
  3098.     default:
  3099.  
  3100. randomchar:
  3101.  
  3102.       if (ident_length > 0) {
  3103.     register HASHNODE *hp;
  3104.  
  3105.     /* We have just seen an identifier end.  If it's a macro, expand it.
  3106.  
  3107.        IDENT_LENGTH is the length of the identifier
  3108.        and HASH is its hash code.
  3109.  
  3110.        The identifier has already been copied to the output,
  3111.        so if it is a macro we must remove it.
  3112.  
  3113.        If REDO_CHAR is 0, the char that terminated the identifier
  3114.        has been skipped in the output and the input.
  3115.        OBP-IDENT_LENGTH-1 points to the identifier.
  3116.        If the identifier is a macro, we must back over the terminator.
  3117.  
  3118.        If REDO_CHAR is 1, the terminating char has already been
  3119.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  3120.  
  3121.     if (!pcp_outfile || pcp_inside_if) {
  3122. startagain:
  3123.       for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  3124.            hp = hp->next) {
  3125.         
  3126.         if (hp->length == ident_length) {
  3127.           int obufp_before_macroname;
  3128.           int op_lineno_before_macroname;
  3129.           register int i = ident_length;
  3130.           register U_CHAR *p = hp->name;
  3131.           register U_CHAR *q = obp - i;
  3132.           int disabled;
  3133.           
  3134.           if (! redo_char)
  3135.         q--;
  3136.           
  3137.           do {        /* All this to avoid a strncmp () */
  3138.         if (*p++ != *q++)
  3139.           goto hashcollision;
  3140.           } while (--i);
  3141.           
  3142.           /* We found a use of a macro name.
  3143.          see if the context shows it is a macro call.  */
  3144.           
  3145.           /* Back up over terminating character if not already done.  */
  3146.           if (! redo_char) {
  3147.         ibp--;
  3148.         obp--;
  3149.           }
  3150.           
  3151.           /* Save this as a displacement from the beginning of the output
  3152.          buffer.  We can not save this as a position in the output
  3153.          buffer, because it may get realloc'ed by RECACHE.  */
  3154.           obufp_before_macroname = (obp - op->buf) - ident_length;
  3155.           op_lineno_before_macroname = op->lineno;
  3156.           
  3157.           if (hp->type == T_PCSTRING) {
  3158.         pcstring_used (hp); /* Mark the definition of this key
  3159.                        as needed, ensuring that it
  3160.                        will be output.  */
  3161.         break;        /* Exit loop, since the key cannot have a
  3162.                    definition any longer.  */
  3163.           }
  3164.  
  3165.           /* Record whether the macro is disabled.  */
  3166.           disabled = hp->type == T_DISABLED;
  3167.           
  3168.           /* This looks like a macro ref, but if the macro was disabled,
  3169.          just copy its name and put in a marker if requested.  */
  3170.           
  3171.           if (disabled) {
  3172. #if 0
  3173.         /* This error check caught useful cases such as
  3174.            #define foo(x,y) bar (x (y,0), y)
  3175.            foo (foo, baz)  */
  3176.         if (traditional)
  3177.           error ("recursive use of macro `%s'", hp->name);
  3178. #endif
  3179.         
  3180.         if (output_marks) {
  3181.           check_expand (op, limit - ibp + 2);
  3182.           *obp++ = '\n';
  3183.           *obp++ = '-';
  3184.         }
  3185.         break;
  3186.           }
  3187.           
  3188.           /* If macro wants an arglist, verify that a '(' follows.
  3189.          first skip all whitespace, copying it to the output
  3190.          after the macro name.  Then, if there is no '(',
  3191.          decide this is not a macro call and leave things that way.  */
  3192.           if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  3193.           && hp->value.defn->nargs >= 0)
  3194.         {
  3195.           U_CHAR *old_ibp = ibp;
  3196.           U_CHAR *old_obp = obp;
  3197.           int old_iln = ip->lineno;
  3198.           int old_oln = op->lineno;
  3199.           
  3200.           while (1) {
  3201.             /* Scan forward over whitespace, copying it to the output.  */
  3202.             if (ibp == limit && ip->macro != 0) {
  3203.               POPMACRO;
  3204.               RECACHE;
  3205.               old_ibp = ibp;
  3206.               old_obp = obp;
  3207.               old_iln = ip->lineno;
  3208.               old_oln = op->lineno;
  3209.             }
  3210.             /* A comment: copy it unchanged or discard it.  */
  3211.             else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
  3212.               if (put_out_comments) {
  3213.             *obp++ = '/';
  3214.             *obp++ = '*';
  3215.               } else if (! traditional) {
  3216.             *obp++ = ' ';
  3217.               }
  3218.               ibp += 2;
  3219.               while (ibp + 1 != limit
  3220.                  && !(ibp[0] == '*' && ibp[1] == '/')) {
  3221.             /* We need not worry about newline-marks,
  3222.                since they are never found in comments.  */
  3223.             if (*ibp == '\n') {
  3224.               /* Newline in a file.  Count it.  */
  3225.               ++ip->lineno;
  3226.               ++op->lineno;
  3227.             }
  3228.             if (put_out_comments)
  3229.               *obp++ = *ibp++;
  3230.             else
  3231.               ibp++;
  3232.               }
  3233.               ibp += 2;
  3234.               if (put_out_comments) {
  3235.             *obp++ = '*';
  3236.             *obp++ = '/';
  3237.               }
  3238.             }
  3239.             else if (is_space[*ibp]) {
  3240.               *obp++ = *ibp++;
  3241.               if (ibp[-1] == '\n') {
  3242.             if (ip->macro == 0) {
  3243.               /* Newline in a file.  Count it.  */
  3244.               ++ip->lineno;
  3245.               ++op->lineno;
  3246.             } else if (!output_marks) {
  3247.               /* A newline mark, and we don't want marks
  3248.                  in the output.  If it is newline-hyphen,
  3249.                  discard it entirely.  Otherwise, it is
  3250.                  newline-whitechar, so keep the whitechar.  */
  3251.               obp--;
  3252.               if (*ibp == '-')
  3253.                 ibp++;
  3254.               else {
  3255.                 if (*ibp == '\n')
  3256.                   ++op->lineno;
  3257.                 *obp++ = *ibp++;
  3258.               }
  3259.             } else {
  3260.               /* A newline mark; copy both chars to the output.  */
  3261.               *obp++ = *ibp++;
  3262.             }
  3263.               }
  3264.             }
  3265.             else break;
  3266.           }
  3267.           if (*ibp != '(') {
  3268.             /* It isn't a macro call.
  3269.                Put back the space that we just skipped.  */
  3270.             ibp = old_ibp;
  3271.             obp = old_obp;
  3272.             ip->lineno = old_iln;
  3273.             op->lineno = old_oln;
  3274.             /* Exit the for loop.  */
  3275.             break;
  3276.           }
  3277.         }
  3278.           
  3279.           /* This is now known to be a macro call.
  3280.          Discard the macro name from the output,
  3281.          along with any following whitespace just copied,
  3282.          but preserve newlines if not outputting marks since this
  3283.          is more likely to do the right thing with line numbers.  */
  3284.           obp = op->buf + obufp_before_macroname;
  3285.           if (output_marks)
  3286.         op->lineno = op_lineno_before_macroname;
  3287.           else {
  3288.         int newlines = op->lineno - op_lineno_before_macroname;
  3289.         while (0 < newlines--)
  3290.           *obp++ = '\n';
  3291.           }
  3292.  
  3293.           /* Prevent accidental token-pasting with a character
  3294.          before the macro call.  */
  3295.           if (!traditional && obp != op->buf
  3296.           && (obp[-1] == '-' || obp[1] == '+' || obp[1] == '&'
  3297.               || obp[-1] == '|' || obp[1] == '<' || obp[1] == '>')) {
  3298.         /* If we are expanding a macro arg, make a newline marker
  3299.            to separate the tokens.  If we are making real output,
  3300.            a plain space will do.  */
  3301.         if (output_marks)
  3302.           *obp++ = '\n';
  3303.         *obp++ = ' ';
  3304.           }
  3305.  
  3306.           /* Expand the macro, reading arguments as needed,
  3307.          and push the expansion on the input stack.  */
  3308.           ip->bufp = ibp;
  3309.           op->bufp = obp;
  3310.           macroexpand (hp, op);
  3311.           
  3312.           /* Reexamine input stack, since macroexpand has pushed
  3313.          a new level on it.  */
  3314.           obp = op->bufp;
  3315.           RECACHE;
  3316.           break;
  3317.         }
  3318. hashcollision:
  3319.         ;
  3320.       }            /* End hash-table-search loop */
  3321.     }
  3322.     ident_length = hash = 0; /* Stop collecting identifier */
  3323.     redo_char = 0;
  3324.     concatenated = 0;
  3325.       }                /* End if (ident_length > 0) */
  3326.     }                /* End switch */
  3327.   }                /* End per-char loop */
  3328.  
  3329.   /* Come here to return -- but first give an error message
  3330.      if there was an unterminated successful conditional.  */
  3331.  ending:
  3332.   if (if_stack != ip->if_stack)
  3333.     {
  3334.       char *str = "unknown";
  3335.  
  3336.       switch (if_stack->type)
  3337.     {
  3338.     case T_IF:
  3339.       str = "if";
  3340.       break;
  3341.     case T_IFDEF:
  3342.       str = "ifdef";
  3343.       break;
  3344.     case T_IFNDEF:
  3345.       str = "ifndef";
  3346.       break;
  3347.     case T_ELSE:
  3348.       str = "else";
  3349.       break;
  3350.     case T_ELIF:
  3351.       str = "elif";
  3352.       break;
  3353.     }
  3354.  
  3355.       error_with_line (line_for_error (if_stack->lineno),
  3356.                "unterminated `#%s' conditional", str);
  3357.   }
  3358.   if_stack = ip->if_stack;
  3359. }
  3360.  
  3361. /*
  3362.  * Rescan a string into a temporary buffer and return the result
  3363.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  3364.  *
  3365.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  3366.  * and insert such markers when appropriate.  See `rescan' for details.
  3367.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  3368.  * before substitution; it is 0 for other uses.
  3369.  */
  3370. static FILE_BUF
  3371. expand_to_temp_buffer (buf, limit, output_marks, assertions)
  3372.      U_CHAR *buf, *limit;
  3373.      int output_marks, assertions;
  3374. {
  3375.   register FILE_BUF *ip;
  3376.   FILE_BUF obuf;
  3377.   int length = limit - buf;
  3378.   U_CHAR *buf1;
  3379.   int odepth = indepth;
  3380.   int save_assertions_flag = assertions_flag;
  3381.  
  3382.   assertions_flag = assertions;
  3383.  
  3384.   if (length < 0)
  3385.     abort ();
  3386.  
  3387.   /* Set up the input on the input stack.  */
  3388.  
  3389.   buf1 = (U_CHAR *) alloca (length + 1);
  3390.   {
  3391.     register U_CHAR *p1 = buf;
  3392.     register U_CHAR *p2 = buf1;
  3393.  
  3394.     while (p1 != limit)
  3395.       *p2++ = *p1++;
  3396.   }
  3397.   buf1[length] = 0;
  3398.  
  3399.   /* Set up to receive the output.  */
  3400.  
  3401.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  3402.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  3403.   obuf.fname = 0;
  3404.   obuf.macro = 0;
  3405.   obuf.free_ptr = 0;
  3406.  
  3407.   CHECK_DEPTH ({return obuf;});
  3408.  
  3409.   ++indepth;
  3410.  
  3411.   ip = &instack[indepth];
  3412.   ip->fname = 0;
  3413.   ip->nominal_fname = 0;
  3414.   ip->system_header_p = 0;
  3415.   ip->macro = 0;
  3416.   ip->free_ptr = 0;
  3417.   ip->length = length;
  3418.   ip->buf = ip->bufp = buf1;
  3419.   ip->if_stack = if_stack;
  3420.  
  3421.   ip->lineno = obuf.lineno = 1;
  3422.  
  3423.   /* Scan the input, create the output.  */
  3424.   rescan (&obuf, output_marks);
  3425.  
  3426.   /* Pop input stack to original state.  */
  3427.   --indepth;
  3428.  
  3429.   if (indepth != odepth)
  3430.     abort ();
  3431.  
  3432.   /* Record the output.  */
  3433.   obuf.length = obuf.bufp - obuf.buf;
  3434.  
  3435.   assertions_flag = save_assertions_flag;
  3436.   return obuf;
  3437. }
  3438.  
  3439. /*
  3440.  * Process a # directive.  Expects IP->bufp to point after the '#', as in
  3441.  * `#define foo bar'.  Passes to the command handler
  3442.  * (do_define, do_include, etc.): the addresses of the 1st and
  3443.  * last chars of the command (starting immediately after the #
  3444.  * keyword), plus op and the keyword table pointer.  If the command
  3445.  * contains comments it is copied into a temporary buffer sans comments
  3446.  * and the temporary buffer is passed to the command handler instead.
  3447.  * Likewise for backslash-newlines.
  3448.  *
  3449.  * Returns nonzero if this was a known # directive.
  3450.  * Otherwise, returns zero, without advancing the input pointer.
  3451.  */
  3452.  
  3453. static int
  3454. handle_directive (ip, op)
  3455.      FILE_BUF *ip, *op;
  3456. {
  3457.   register U_CHAR *bp, *cp;
  3458.   register struct directive *kt;
  3459.   register int ident_length;
  3460.   U_CHAR *resume_p;
  3461.  
  3462.   /* Nonzero means we must copy the entire command
  3463.      to get rid of comments or backslash-newlines.  */
  3464.   int copy_command = 1; /* An AMB Hack , original was 0. */
  3465.  
  3466.   U_CHAR *ident, *after_ident;
  3467.  
  3468.   bp = ip->bufp;
  3469.  
  3470.   /* Record where the directive started.  do_xifdef needs this.  */
  3471.   directive_start = bp - 1;
  3472.  
  3473.   /* Skip whitespace and \-newline.  */
  3474.   while (1) {
  3475.     if (is_hor_space[*bp]) {
  3476.       if ((*bp == '\f' || *bp == '\v') && pedantic)
  3477.     pedwarn ("%s in preprocessing directive",
  3478.          *bp == '\f' ? "formfeed" : "vertical tab");
  3479.       bp++;
  3480.     } else if (*bp == '/' && (bp[1] == '*'
  3481.                   || (cplusplus_comments && bp[1] == '/'))) {
  3482.       ip->bufp = bp + 2;
  3483.       skip_to_end_of_comment (ip, &ip->lineno, 0);
  3484.       bp = ip->bufp;
  3485.     } else if (*bp == '\\' && bp[1] == '\n') {
  3486.       bp += 2; ip->lineno++;
  3487.     } else break;
  3488.   }
  3489.  
  3490.   /* Now find end of directive name.
  3491.      If we encounter a backslash-newline, exchange it with any following
  3492.      symbol-constituents so that we end up with a contiguous name.  */
  3493.  
  3494.   cp = bp;
  3495.   while (1) {
  3496.     if (is_idchar[*cp])
  3497.       cp++;
  3498.     else {
  3499.       if (*cp == '\\' && cp[1] == '\n')
  3500.     name_newline_fix (cp);
  3501.       if (is_idchar[*cp])
  3502.     cp++;
  3503.       else break;
  3504.     }
  3505.   }
  3506.   ident_length = cp - bp;
  3507.   ident = bp;
  3508.   after_ident = cp;
  3509.  
  3510.   /* A line of just `#' becomes blank.  */
  3511.  
  3512.   if (ident_length == 0 && *after_ident == '\n') {
  3513.     ip->bufp = after_ident;
  3514.     return 1;
  3515.   }
  3516.  
  3517.   if (ident_length == 0 || !is_idstart[*ident]) {
  3518.     U_CHAR *p = ident;
  3519.     while (is_idchar[*p]) {
  3520.       if (*p < '0' || *p > '9')
  3521.     break;
  3522.       p++;
  3523.     }
  3524.     /* Handle # followed by a line number.  */
  3525.     if (p != ident && !is_idchar[*p]) {
  3526.       static struct directive line_directive_table[] = {
  3527.     {  4, do_line, "line", T_LINE},
  3528.       };
  3529.       if (pedantic)
  3530.     pedwarn ("`#' followed by integer");
  3531.       after_ident = ident;
  3532.       kt = line_directive_table;
  3533.       goto old_linenum;
  3534.     }
  3535.  
  3536.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  3537.     if (p == ident) {
  3538.       while (*p == '#' || is_hor_space[*p]) p++;
  3539.       if (*p == '\n') {
  3540.     if (pedantic && !lang_asm)
  3541.       warning ("invalid preprocessor directive");
  3542.     return 0;
  3543.       }
  3544.     }
  3545.  
  3546.     if (!lang_asm)
  3547.       error ("invalid preprocessor directive name");
  3548.  
  3549.     return 0;
  3550.   }
  3551.  
  3552.   /*
  3553.    * Decode the keyword and call the appropriate expansion
  3554.    * routine, after moving the input pointer up to the next line.
  3555.    */
  3556.   for (kt = directive_table; kt->length > 0; kt++) {
  3557.     if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
  3558.       register U_CHAR *buf;
  3559.       register U_CHAR *limit;
  3560.       int unterminated;
  3561.       int junk;
  3562.       int *already_output;
  3563.  
  3564.       /* Nonzero means do not delete comments within the directive.
  3565.      #define needs this when -traditional.  */
  3566.       int keep_comments;
  3567.  
  3568.     old_linenum:
  3569.  
  3570.       limit = ip->buf + ip->length;
  3571.       unterminated = 0;
  3572.       already_output = 0;
  3573.       keep_comments = traditional && kt->traditional_comments;
  3574.       /* #import is defined only in Objective C, or when on the NeXT.  */
  3575.       if (kt->type == T_IMPORT && !(objc || lookup ("__NeXT__", -1, -1)))
  3576.     break;
  3577.  
  3578.       /* Find the end of this command (first newline not backslashed
  3579.      and not in a string or comment).
  3580.      Set COPY_COMMAND if the command must be copied
  3581.      (it contains a backslash-newline or a comment).  */
  3582.  
  3583.       buf = bp = after_ident;
  3584.       while (bp < limit) {
  3585.     register U_CHAR c = *bp++;
  3586.     switch (c) {
  3587.     case '\\':
  3588.       if (bp < limit) {
  3589.         if (*bp == '\n') {
  3590.           ip->lineno++;
  3591.           copy_command = 1;
  3592.           bp++;
  3593.         } else if (traditional)
  3594.           bp++;
  3595.       }
  3596.       break;
  3597.  
  3598.     case '\'':
  3599.     case '\"':
  3600.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated);
  3601.       /* Don't bother calling the directive if we already got an error
  3602.          message due to unterminated string.  Skip everything and pretend
  3603.          we called the directive.  */
  3604.       if (unterminated) {
  3605.         if (traditional) {
  3606.           /* Traditional preprocessing permits unterminated strings.  */
  3607.           ip->bufp = bp;
  3608.           goto endloop1;
  3609.         }
  3610.         ip->bufp = bp;
  3611.         return 1;
  3612.       }
  3613.       break;
  3614.  
  3615.       /* <...> is special for #include.  */
  3616.     case '<':
  3617.       if (!kt->angle_brackets)
  3618.         break;
  3619.       while (bp < limit && *bp != '>' && *bp != '\n') {
  3620.         if (*bp == '\\' && bp[1] == '\n') {
  3621.           ip->lineno++;
  3622.           copy_command = 1;
  3623.           bp++;
  3624.         }
  3625.         bp++;
  3626.       }
  3627.       break;
  3628.  
  3629.     case '/':
  3630.       if (*bp == '\\' && bp[1] == '\n')
  3631.         newline_fix (bp);
  3632.       if (*bp == '*'
  3633.           || (cplusplus_comments && *bp == '/')) {
  3634.         U_CHAR *obp = bp - 1;
  3635.         ip->bufp = bp + 1;
  3636.         skip_to_end_of_comment (ip, &ip->lineno, 0);
  3637.         bp = ip->bufp;
  3638.         /* No need to copy the command because of a comment at the end;
  3639.            just don't include the comment in the directive.  */
  3640.         if (bp == limit || *bp == '\n') {
  3641.           /* bp = obp; An AMB Hack, originalwas not commented out. */
  3642.           goto endloop1;
  3643.         }
  3644.         /* Don't remove the comments if -traditional.  */
  3645.         if (! keep_comments)
  3646.           copy_command++;
  3647.       }
  3648.       break;
  3649.  
  3650.     case '\f':
  3651.     case '\v':
  3652.       if (pedantic)
  3653.         pedwarn ("%s in preprocessing directive",
  3654.              c == '\f' ? "formfeed" : "vertical tab");
  3655.       break;
  3656.  
  3657.     case '\n':
  3658.       --bp;        /* Point to the newline */
  3659.       ip->bufp = bp;
  3660.       goto endloop1;
  3661.     }
  3662.       }
  3663.       ip->bufp = bp;
  3664.  
  3665.     endloop1:
  3666.       resume_p = ip->bufp;
  3667.       /* BP is the end of the directive.
  3668.      RESUME_P is the next interesting data after the directive.
  3669.      A comment may come between.  */
  3670.  
  3671.       /* If a directive should be copied through, and -E was given,
  3672.      pass it through before removing comments.  */
  3673.       if (!no_output && kt->pass_thru && put_out_comments) {
  3674.         int len;
  3675.  
  3676.     /* Output directive name.  */
  3677.         check_expand (op, kt->length + 2);
  3678.     /* Make sure # is at the start of a line */
  3679.     if (op->bufp > op->buf && op->bufp[-1] != '\n') {
  3680.       op->lineno++;
  3681.       *op->bufp++ = '\n';
  3682.     }
  3683.         *op->bufp++ = '#';
  3684.         bcopy (kt->name, op->bufp, kt->length);
  3685.         op->bufp += kt->length;
  3686.  
  3687.     /* Output arguments.  */
  3688.     len = (bp - buf);
  3689.     check_expand (op, len);
  3690.     bcopy (buf, (char *) op->bufp, len);
  3691.     op->bufp += len;
  3692.     /* Take account of any (escaped) newlines just output.  */
  3693.     while (--len >= 0)
  3694.       if (buf[len] == '\n')
  3695.         op->lineno++;
  3696.  
  3697.     already_output = &junk;
  3698.       }                /* Don't we need a newline or #line? */
  3699.  
  3700.       if (copy_command) {
  3701.     register U_CHAR *xp = buf;
  3702.     /* Need to copy entire command into temp buffer before dispatching */
  3703.  
  3704.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
  3705.                           some slop */
  3706.     buf = cp;
  3707.  
  3708.     /* Copy to the new buffer, deleting comments
  3709.        and backslash-newlines (and whitespace surrounding the latter).  */
  3710.  
  3711.     while (xp < bp) {
  3712.       register U_CHAR c = *xp++;
  3713.       *cp++ = c;
  3714.  
  3715.       switch (c) {
  3716.       case '\n':
  3717.         abort ();  /* A bare newline should never part of the line.  */
  3718.         break;
  3719.  
  3720.         /* <...> is special for #include.  */
  3721.       case '<':
  3722.         if (!kt->angle_brackets)
  3723.           break;
  3724.         while (xp < bp && c != '>') {
  3725.           c = *xp++;
  3726.           if (c == '\\' && xp < bp && *xp == '\n')
  3727.         xp++;
  3728.           else
  3729.         *cp++ = c;
  3730.         }
  3731.         break;
  3732.  
  3733.       case '\\':
  3734.         if (*xp == '\n') {
  3735.           xp++;
  3736.           cp--;
  3737.           if (cp != buf && is_space[cp[-1]]) {
  3738.         while (cp != buf && is_space[cp[-1]]) cp--;
  3739.         cp++;
  3740.         SKIP_WHITE_SPACE (xp);
  3741.           } else if (is_space[*xp]) {
  3742.         *cp++ = *xp++;
  3743.         SKIP_WHITE_SPACE (xp);
  3744.           }
  3745.         } else {
  3746.           *cp++ = *xp++;
  3747.         }
  3748.         break;
  3749.  
  3750.       case '\'':
  3751.       case '\"':
  3752.         {
  3753.           register U_CHAR *bp1
  3754.         = skip_quoted_string (xp - 1, bp, ip->lineno,
  3755.                       NULL_PTR, NULL_PTR, NULL_PTR);
  3756.           while (xp != bp1)
  3757.         if (*xp == '\\') {
  3758.           if (*++xp != '\n')
  3759.             *cp++ = '\\';
  3760.           else
  3761.             xp++;
  3762.         } else
  3763.           *cp++ = *xp++;
  3764.         }
  3765.         break;
  3766.  
  3767.       case '/':
  3768.         if (*xp == '*'
  3769.         || (cplusplus_comments && *xp == '/')) {
  3770.           ip->bufp = xp + 1;
  3771.           /* If we already copied the command through,
  3772.          already_output != 0 prevents outputting comment now.  */
  3773.           skip_to_end_of_comment (ip, already_output, 0);
  3774.           if (keep_comments)
  3775.         while (xp != ip->bufp)
  3776.           *cp++ = *xp++;
  3777.           /* Delete or replace the slash.  */
  3778.           else if (traditional)
  3779.         cp--;
  3780.           else
  3781.         cp[-1] = ' ';
  3782.           xp = ip->bufp;
  3783.         }
  3784.       }
  3785.     }
  3786.  
  3787.     /* Null-terminate the copy.  */
  3788.  
  3789.     *cp = 0;
  3790.       } else
  3791.     cp = bp;
  3792.  
  3793.       ip->bufp = resume_p;
  3794.  
  3795.       /* Some directives should be written out for cc1 to process,
  3796.      just as if they were not defined.  And sometimes we're copying
  3797.      definitions through.  */
  3798.  
  3799.       if (!no_output && already_output == 0
  3800.       && (kt->pass_thru
  3801.           || (kt->type == T_DEFINE
  3802.           && (dump_macros == dump_names
  3803.               || dump_macros == dump_definitions)))) {
  3804.         int len;
  3805.  
  3806.     /* Output directive name.  */
  3807.         check_expand (op, kt->length + 1);
  3808.         *op->bufp++ = '#';
  3809.         bcopy (kt->name, (char *) op->bufp, kt->length);
  3810.         op->bufp += kt->length;
  3811.  
  3812.     if (kt->pass_thru || dump_macros == dump_definitions) {
  3813.       /* Output arguments.  */
  3814.       len = (cp - buf);
  3815.       check_expand (op, len);
  3816.       bcopy (buf, (char *) op->bufp, len);
  3817.       op->bufp += len;
  3818.     } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
  3819.       U_CHAR *xp = buf;
  3820.       U_CHAR *yp;
  3821.       SKIP_WHITE_SPACE (xp);
  3822.       yp = xp;
  3823.       while (is_idchar[*xp]) xp++;
  3824.       len = (xp - yp);
  3825.       check_expand (op, len + 1);
  3826.       *op->bufp++ = ' ';
  3827.       bcopy (yp, op->bufp, len);
  3828.       op->bufp += len;
  3829.     }
  3830.       }                /* Don't we need a newline or #line? */
  3831.  
  3832.       /* Call the appropriate command handler.  buf now points to
  3833.      either the appropriate place in the input buffer, or to
  3834.      the temp buffer if it was necessary to make one.  cp
  3835.      points to the first char after the contents of the (possibly
  3836.      copied) command, in either case. */
  3837.       (*kt->func) (buf, cp, op, kt);
  3838.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  3839.  
  3840.       return 1;
  3841.     }
  3842.   }
  3843.  
  3844.   /* It is deliberate that we don't warn about undefined directives.
  3845.      That is the responsibility of cc1.  */
  3846.   return 0;
  3847. }
  3848.  
  3849. static struct tm *
  3850. timestamp ()
  3851. {
  3852.   static struct tm *timebuf;
  3853.   if (!timebuf) {
  3854.     time_t t = time ((time_t *)0);
  3855.     timebuf = localtime (&t);
  3856.   }
  3857.   return timebuf;
  3858. }
  3859.  
  3860. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  3861.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  3862.                 };
  3863.  
  3864. /*
  3865.  * expand things like __FILE__.  Place the expansion into the output
  3866.  * buffer *without* rescanning.
  3867.  */
  3868.  
  3869. static void
  3870. special_symbol (hp, op)
  3871.      HASHNODE *hp;
  3872.      FILE_BUF *op;
  3873. {
  3874.   char *buf;
  3875.   int i, len;
  3876.   int true_indepth;
  3877.   FILE_BUF *ip = NULL;
  3878.   struct tm *timebuf;
  3879.  
  3880.   int paren = 0;        /* For special `defined' keyword */
  3881.  
  3882.   if (pcp_outfile && pcp_inside_if
  3883.       && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
  3884.     error ("Predefined macro `%s' used inside `#if' during precompilation",
  3885.        hp->name);
  3886.     
  3887.   for (i = indepth; i >= 0; i--)
  3888.     if (instack[i].fname != NULL) {
  3889.       ip = &instack[i];
  3890.       break;
  3891.     }
  3892.   if (ip == NULL) {
  3893.     error ("cccp error: not in any file?!");
  3894.     return;            /* the show must go on */
  3895.   }
  3896.  
  3897.   switch (hp->type) {
  3898.   case T_FILE:
  3899.   case T_BASE_FILE:
  3900.     {
  3901.       char *string;
  3902.       if (hp->type == T_FILE)
  3903.     string = ip->nominal_fname;
  3904.       else
  3905.     string = instack[0].nominal_fname;
  3906.  
  3907.       if (string)
  3908.     {
  3909.       buf = (char *) alloca (3 + 4 * strlen (string));
  3910.       quote_string (buf, string);
  3911.     }
  3912.       else
  3913.     buf = "\"\"";
  3914.  
  3915.       break;
  3916.     }
  3917.  
  3918.   case T_INCLUDE_LEVEL:
  3919.     true_indepth = 0;
  3920.     for (i = indepth; i >= 0; i--)
  3921.       if (instack[i].fname != NULL)
  3922.         true_indepth++;
  3923.  
  3924.     buf = (char *) alloca (8);    /* Eight bytes ought to be more than enough */
  3925.     sprintf (buf, "%d", true_indepth - 1);
  3926.     break;
  3927.  
  3928.   case T_VERSION:
  3929.     buf = (char *) alloca (3 + strlen (version_string));
  3930.     sprintf (buf, "\"%s\"", version_string);
  3931.     break;
  3932.  
  3933. #ifndef NO_BUILTIN_SIZE_TYPE
  3934.   case T_SIZE_TYPE:
  3935.     buf = SIZE_TYPE;
  3936.     break;
  3937. #endif
  3938.  
  3939. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  3940.   case T_PTRDIFF_TYPE:
  3941.     buf = PTRDIFF_TYPE;
  3942.     break;
  3943. #endif
  3944.  
  3945.   case T_WCHAR_TYPE:
  3946.     buf = wchar_type;
  3947.     break;
  3948.  
  3949.   case T_USER_LABEL_PREFIX_TYPE:
  3950.     buf = USER_LABEL_PREFIX;
  3951.     break;
  3952.  
  3953.   case T_REGISTER_PREFIX_TYPE:
  3954.     buf = REGISTER_PREFIX;
  3955.     break;
  3956.  
  3957.   case T_CONST:
  3958.     buf = (char *) alloca (4 * sizeof (int));
  3959.     sprintf (buf, "%d", hp->value.ival);
  3960.     if (pcp_inside_if && pcp_outfile)
  3961.       /* Output a precondition for this macro use */
  3962.       fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
  3963.     break;
  3964.  
  3965.   case T_SPECLINE:
  3966.     buf = (char *) alloca (10);
  3967.     sprintf (buf, "%d", ip->lineno);
  3968.     break;
  3969.  
  3970.   case T_DATE:
  3971.   case T_TIME:
  3972.     buf = (char *) alloca (20);
  3973.     timebuf = timestamp ();
  3974.     if (hp->type == T_DATE)
  3975.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  3976.           timebuf->tm_mday, timebuf->tm_year + 1900);
  3977.     else
  3978.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  3979.           timebuf->tm_sec);
  3980.     break;
  3981.  
  3982.   case T_SPEC_DEFINED:
  3983.     buf = " 0 ";        /* Assume symbol is not defined */
  3984.     ip = &instack[indepth];
  3985.     SKIP_WHITE_SPACE (ip->bufp);
  3986.     if (*ip->bufp == '(') {
  3987.       paren++;
  3988.       ip->bufp++;            /* Skip over the paren */
  3989.       SKIP_WHITE_SPACE (ip->bufp);
  3990.     }
  3991.  
  3992.     if (!is_idstart[*ip->bufp])
  3993.       goto oops;
  3994.     if (hp = lookup (ip->bufp, -1, -1)) {
  3995.       if (pcp_outfile && pcp_inside_if
  3996.       && (hp->type == T_CONST
  3997.           || (hp->type == T_MACRO && hp->value.defn->predefined)))
  3998.     /* Output a precondition for this macro use. */
  3999.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  4000.       buf = " 1 ";
  4001.     }
  4002.     else
  4003.       if (pcp_outfile && pcp_inside_if)    {
  4004.     /* Output a precondition for this macro use */
  4005.     U_CHAR *cp = ip->bufp;
  4006.     fprintf (pcp_outfile, "#undef ");
  4007.     while (is_idchar[*cp]) /* Ick! */
  4008.       fputc (*cp++, pcp_outfile);
  4009.     putc ('\n', pcp_outfile);
  4010.       }
  4011.     while (is_idchar[*ip->bufp])
  4012.       ++ip->bufp;
  4013.     SKIP_WHITE_SPACE (ip->bufp);
  4014.     if (paren) {
  4015.       if (*ip->bufp != ')')
  4016.     goto oops;
  4017.       ++ip->bufp;
  4018.     }
  4019.     break;
  4020.  
  4021. oops:
  4022.  
  4023.     error ("`defined' without an identifier");
  4024.     break;
  4025.  
  4026.   default:
  4027.     error ("cccp error: invalid special hash type"); /* time for gdb */
  4028.     abort ();
  4029.   }
  4030.   len = strlen (buf);
  4031.   check_expand (op, len);
  4032.   bcopy (buf, (char *) op->bufp, len);
  4033.   op->bufp += len;
  4034.  
  4035.   return;
  4036. }
  4037.  
  4038.  
  4039. /* Routines to handle #directives */
  4040.  
  4041. /* Handle #include and #import.
  4042.    This function expects to see "fname" or <fname> on the input.  */
  4043.  
  4044. static int
  4045. do_include (buf, limit, op, keyword)
  4046.      U_CHAR *buf, *limit;
  4047.      FILE_BUF *op;
  4048.      struct directive *keyword;
  4049. {
  4050.   int importing = (keyword->type == T_IMPORT);
  4051.   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
  4052.   static int import_warning = 0;
  4053.   char *fname;        /* Dynamically allocated fname buffer */
  4054.   char *pcftry;
  4055.   char *pcfname;
  4056.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  4057.  
  4058.   struct file_name_list *search_start = include; /* Chain of dirs to search */
  4059.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  4060.   struct file_name_list *searchptr = 0;
  4061.   int flen;
  4062.  
  4063.   int f;            /* file number */
  4064.  
  4065.   int retried = 0;        /* Have already tried macro
  4066.                    expanding the include line*/
  4067.   FILE_BUF trybuf;        /* It got expanded into here */
  4068.   int angle_brackets = 0;    /* 0 for "...", 1 for <...> */
  4069.   int pcf = -1;
  4070.   char *pcfbuf;
  4071.   int pcfbuflimit;
  4072.   int pcfnum;
  4073.   f= -1;            /* JF we iz paranoid! */
  4074.  
  4075.   if (importing && warn_import && !inhibit_warnings
  4076.       && !instack[indepth].system_header_p && !import_warning) {
  4077.     import_warning = 1;
  4078.     warning ("using `#import' is not recommended");
  4079.     fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
  4080.     fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
  4081.     fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
  4082.     fprintf (stderr, "  #ifndef _FOO_H_INCLUDED\n");
  4083.     fprintf (stderr, "  #define _FOO_H_INCLUDED\n");
  4084.     fprintf (stderr, "  ... <real contents of file> ...\n");
  4085.     fprintf (stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
  4086.     fprintf (stderr, "Then users can use `#include' any number of times.\n");
  4087.     fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
  4088.     fprintf (stderr, "when it is equipped with such a conditional.\n");
  4089.   }
  4090.  
  4091. get_filename:
  4092.  
  4093.   fbeg = buf;
  4094.   SKIP_WHITE_SPACE (fbeg);
  4095.   /* Discard trailing whitespace so we can easily see
  4096.      if we have parsed all the significant chars we were given.  */
  4097.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  4098.  
  4099.   switch (*fbeg++) {
  4100.   case '\"':
  4101.     {
  4102.       FILE_BUF *fp;
  4103.       /* Copy the operand text, concatenating the strings.  */
  4104.       {
  4105.     U_CHAR *fin = fbeg;
  4106.     fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
  4107.     fend = fbeg;
  4108.     while (fin != limit) {
  4109.       while (fin != limit && *fin != '\"')
  4110.         *fend++ = *fin++;
  4111.       fin++;
  4112.       if (fin == limit)
  4113.         break;
  4114.       /* If not at the end, there had better be another string.  */
  4115.       /* Skip just horiz space, and don't go past limit.  */
  4116.       while (fin != limit && is_hor_space[*fin]) fin++;
  4117.       if (fin != limit && *fin == '\"')
  4118.         fin++;
  4119.       else
  4120.         goto fail;
  4121.     }
  4122.       }
  4123.       *fend = 0;
  4124.  
  4125.       /* We have "filename".  Figure out directory this source
  4126.      file is coming from and put it on the front of the list. */
  4127.  
  4128.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  4129.       if (ignore_srcdir) break;
  4130.  
  4131.       for (fp = &instack[indepth]; fp >= instack; fp--)
  4132.     {
  4133.       int n;
  4134.       char *ep,*nam;
  4135.  
  4136.       if ((nam = fp->nominal_fname) != NULL) {
  4137.         /* Found a named file.  Figure out dir of the file,
  4138.            and put it in front of the search list.  */
  4139.         dsp[0].next = search_start;
  4140.         search_start = dsp;
  4141. #ifndef VMS
  4142.         ep = rindex (nam, '/');
  4143. #else                /* VMS */
  4144.         ep = rindex (nam, ']');
  4145.         if (ep == NULL) ep = rindex (nam, '>');
  4146.         if (ep == NULL) ep = rindex (nam, ':');
  4147.         if (ep != NULL) ep++;
  4148. #endif                /* VMS */
  4149.         if (ep != NULL) {
  4150.           n = ep - nam;
  4151.           dsp[0].fname = (char *) alloca (n + 1);
  4152.           strncpy (dsp[0].fname, nam, n);
  4153.           dsp[0].fname[n] = '\0';
  4154.           if (n + INCLUDE_LEN_FUDGE > max_include_len)
  4155.         max_include_len = n + INCLUDE_LEN_FUDGE;
  4156.         } else {
  4157.           dsp[0].fname = 0; /* Current directory */
  4158.         }
  4159.         dsp[0].got_name_map = 0;
  4160.         break;
  4161.       }
  4162.     }
  4163.       break;
  4164.     }
  4165.  
  4166.   case '<':
  4167.     fend = fbeg;
  4168.     while (fend != limit && *fend != '>') fend++;
  4169.     if (*fend == '>' && fend + 1 == limit) {
  4170.       angle_brackets = 1;
  4171.       /* If -I-, start with the first -I dir after the -I-.  */
  4172.       if (first_bracket_include)
  4173.     search_start = first_bracket_include;
  4174.       break;
  4175.     }
  4176.     goto fail;
  4177.  
  4178.   default:
  4179. #ifdef VMS
  4180.     /*
  4181.      * Support '#include xyz' like VAX-C to allow for easy use of all the
  4182.      * decwindow include files. It defaults to '#include <xyz.h>' (so the
  4183.      * code from case '<' is repeated here) and generates a warning.
  4184.      */
  4185.     if (isalpha(*(--fbeg))) {
  4186.       fend = fbeg;
  4187.       while (fend != limit && (!isspace(*fend))) fend++;
  4188.       warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
  4189.       if (fend  == limit) {
  4190.     angle_brackets = 1;
  4191.     /* If -I-, start with the first -I dir after the -I-.  */
  4192.     if (first_bracket_include)
  4193.       search_start = first_bracket_include;
  4194.     break;
  4195.       }
  4196.     }
  4197. #endif
  4198.  
  4199.   fail:
  4200.     if (retried) {
  4201.       error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
  4202.       return 0;
  4203.     } else {
  4204.       trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  4205.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  4206.       bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  4207.       limit = buf + (trybuf.bufp - trybuf.buf);
  4208.       free (trybuf.buf);
  4209.       retried++;
  4210.       goto get_filename;
  4211.     }
  4212.   }
  4213.  
  4214.   /* For #include_next, skip in the search path
  4215.      past the dir in which the containing file was found.  */
  4216.   if (skip_dirs) {
  4217.     FILE_BUF *fp;
  4218.     for (fp = &instack[indepth]; fp >= instack; fp--)
  4219.       if (fp->fname != NULL) {
  4220.     /* fp->dir is null if the containing file was specified
  4221.        with an absolute file name.  In that case, don't skip anything.  */
  4222.     if (fp->dir)
  4223.       search_start = fp->dir->next;
  4224.     break;
  4225.       }
  4226.   }
  4227.  
  4228.   flen = fend - fbeg;
  4229.  
  4230.   if (flen == 0)
  4231.     {
  4232.       error ("empty file name in `#%s'", keyword->name);
  4233.       return 0;
  4234.     }
  4235.  
  4236.   /* Allocate this permanently, because it gets stored in the definitions
  4237.      of macros.  */
  4238.   fname = (char *) xmalloc (max_include_len + flen + 4);
  4239.   /* + 2 above for slash and terminating null.  */
  4240.   /* + 2 added for '.h' on VMS (to support '#include filename') */
  4241.  
  4242.   /* If specified file name is absolute, just open it.  */
  4243.  
  4244.   if (*fbeg == '/') {
  4245.     strncpy (fname, fbeg, flen);
  4246.     fname[flen] = 0;
  4247.     if (redundant_include_p (fname))
  4248.       return 0;
  4249.     if (importing)
  4250.       f = lookup_import (fname, NULL_PTR);
  4251.     else
  4252.       f = open_include_file (fname, NULL_PTR);
  4253.     if (f == -2)
  4254.       return 0;        /* Already included this file */
  4255.   } else {
  4256.     /* Search directory path, trying to open the file.
  4257.        Copy each filename tried into FNAME.  */
  4258.  
  4259.     for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
  4260.       if (searchptr->fname) {
  4261.     /* The empty string in a search path is ignored.
  4262.        This makes it possible to turn off entirely
  4263.        a standard piece of the list.  */
  4264.     if (searchptr->fname[0] == 0)
  4265.       continue;
  4266.     strcpy (fname, searchptr->fname);
  4267.     strcat (fname, "/");
  4268.     fname[strlen (fname) + flen] = 0;
  4269.       } else {
  4270.     fname[0] = 0;
  4271.       }
  4272.       strncat (fname, fbeg, flen);
  4273. #ifdef VMS
  4274.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  4275.          full VMS file specification */
  4276.       if (searchptr->fname && (searchptr->fname[0] != 0)) {
  4277.     /* Fix up the filename */
  4278.     hack_vms_include_specification (fname);
  4279.       } else {
  4280.           /* This is a normal VMS filespec, so use it unchanged.  */
  4281.     strncpy (fname, fbeg, flen);
  4282.     fname[flen] = 0;
  4283.     /* if it's '#include filename', add the missing .h */
  4284.     if (index(fname,'.')==NULL) {
  4285.       strcat (fname, ".h");
  4286.     }
  4287.       }
  4288. #endif /* VMS */
  4289.       if (importing)
  4290.     f = lookup_import (fname, searchptr);
  4291.       else
  4292.     f = open_include_file (fname, searchptr);
  4293.       if (f == -2)
  4294.     return 0;            /* Already included this file */
  4295. #ifdef EACCES
  4296.       else if (f == -1 && errno == EACCES)
  4297.     warning ("Header file %s exists, but is not readable", fname);
  4298. #endif
  4299.       if (redundant_include_p (fname)) {
  4300.     close (f);
  4301.     return 0;
  4302.       }
  4303.       if (f >= 0)
  4304.     break;
  4305.     }
  4306.   }
  4307.  
  4308.   if (f < 0) {
  4309.     /* A file that was not found.  */
  4310.  
  4311.     strncpy (fname, fbeg, flen);
  4312.     fname[flen] = 0;
  4313.     /* If generating dependencies and -MG was specified, we assume missing
  4314.        files are leaf files, living in the same directory as the source file
  4315.        or other similar place; these missing files may be generated from
  4316.        other files and may not exist yet (eg: y.tab.h).  */
  4317.     if (print_deps_missing_files
  4318.     && print_deps > (angle_brackets || (system_include_depth > 0)))
  4319.       {
  4320.     /* If it was requested as a system header file,
  4321.        then assume it belongs in the first place to look for such.  */
  4322.     if (angle_brackets)
  4323.       {
  4324.         for (searchptr = search_start; searchptr; searchptr = searchptr->next)
  4325.           {
  4326.         if (searchptr->fname)
  4327.           {
  4328.             char *p;
  4329.  
  4330.             if (searchptr->fname[0] == 0)
  4331.               continue;
  4332.             p = xmalloc (strlen (searchptr->fname)
  4333.                  + strlen (fname) + 2);
  4334.             strcpy (p, searchptr->fname);
  4335.             strcat (p, "/");
  4336.             strcat (p, fname);
  4337.             deps_output (p, ' ');
  4338.             break;
  4339.           }
  4340.           }
  4341.       }
  4342.     else
  4343.       {
  4344.         /* Otherwise, omit the directory, as if the file existed
  4345.            in the directory with the source.  */
  4346.         deps_output (fname, ' ');
  4347.       }
  4348.       }
  4349.     /* If -M was specified, and this header file won't be added to the
  4350.        dependency list, then don't count this as an error, because we can
  4351.        still produce correct output.  Otherwise, we can't produce correct
  4352.        output, because there may be dependencies we need inside the missing
  4353.        file, and we don't know what directory this missing file exists in.  */
  4354.     else if (print_deps
  4355.     && (print_deps <= (angle_brackets || (system_include_depth > 0))))
  4356.       warning ("No include path in which to find %s", fname);
  4357.     else if (search_start)
  4358.       error_from_errno (fname);
  4359.     else
  4360.       error ("No include path in which to find %s", fname);
  4361.   } else {
  4362.     struct stat stat_f;
  4363.  
  4364.     /* Check to see if this include file is a once-only include file.
  4365.        If so, give up.  */
  4366.  
  4367.     struct file_name_list* ptr;
  4368.  
  4369.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  4370.       if (!strcmp (ptr->fname, fname)) {
  4371.     close (f);
  4372.         return 0;                /* This file was once'd. */
  4373.       }
  4374.     }
  4375.  
  4376.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  4377.       if (!strcmp (ptr->fname, fname))
  4378.         break;                /* This file was included before. */
  4379.     }
  4380.  
  4381.     if (ptr == 0) {
  4382.       /* This is the first time for this file.  */
  4383.       /* Add it to list of files included.  */
  4384.  
  4385.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  4386.       ptr->control_macro = 0;
  4387.       ptr->c_system_include_path = 0;
  4388.       ptr->next = all_include_files;
  4389.       all_include_files = ptr;
  4390.       ptr->fname = savestring (fname);
  4391.       ptr->got_name_map = 0;
  4392.  
  4393.       /* For -M, add this file to the dependencies.  */
  4394.       if (print_deps > (angle_brackets || (system_include_depth > 0)))
  4395.     deps_output (fname, ' ');
  4396.     }   
  4397.  
  4398.     /* Handle -H option.  */
  4399.     if (print_include_names) {
  4400.       output_dots (stderr, indepth);
  4401.       fprintf (stderr, "%s\n", fname);
  4402.     }
  4403.  
  4404.     if (angle_brackets)
  4405.       system_include_depth++;
  4406.  
  4407.     /* Actually process the file.  */
  4408.     add_import (f, fname);    /* Record file on "seen" list for #import. */
  4409.  
  4410.     pcftry = (char *) alloca (strlen (fname) + 30);
  4411.     pcfbuf = 0;
  4412.     pcfnum = 0;
  4413.  
  4414.     fstat (f, &stat_f);
  4415.  
  4416.     if (!no_precomp)
  4417.       do {
  4418.     sprintf (pcftry, "%s%d", fname, pcfnum++);
  4419.     
  4420.     pcf = open (pcftry, O_RDONLY, 0666);
  4421.     if (pcf != -1)
  4422.       {
  4423.         struct stat s;
  4424.  
  4425.         fstat (pcf, &s);
  4426.         if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
  4427.               sizeof (s.st_ino))
  4428.         || stat_f.st_dev != s.st_dev)
  4429.           {
  4430.         pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
  4431.         /* Don't need it any more.  */
  4432.         close (pcf);
  4433.           }
  4434.         else
  4435.           {
  4436.         /* Don't need it at all.  */
  4437.         close (pcf);
  4438.         break;
  4439.           }
  4440.       }
  4441.       } while (pcf != -1 && !pcfbuf);
  4442.     
  4443.     /* Actually process the file */
  4444.     if (pcfbuf) {
  4445.       pcfname = xmalloc (strlen (pcftry) + 1);
  4446.       strcpy (pcfname, pcftry);
  4447.       pcfinclude (pcfbuf, pcfbuflimit, fname, op);
  4448.     }
  4449.     else
  4450.       finclude (f, fname, op, angle_brackets , searchptr); /* An AMB  Hack, orignal had is_system_include(fname) instead of angle_brackets. */
  4451.  
  4452.     if (angle_brackets)
  4453.       system_include_depth--;
  4454.   }
  4455.   return 0;
  4456. }
  4457.  
  4458. /* Return nonzero if there is no need to include file NAME
  4459.    because it has already been included and it contains a conditional
  4460.    to make a repeated include do nothing.  */
  4461.  
  4462. static int
  4463. redundant_include_p (name)
  4464.      char *name;
  4465. {
  4466.   struct file_name_list *l = all_include_files;
  4467.   for (; l; l = l->next)
  4468.     if (! strcmp (name, l->fname)
  4469.     && l->control_macro
  4470.     && lookup (l->control_macro, -1, -1))
  4471.       return 1;
  4472.   return 0;
  4473. }
  4474.  
  4475. /* Return nonzero if the given FILENAME is an absolute pathname which
  4476.    designates a file within one of the known "system" include file
  4477.    directories.  We assume here that if the given FILENAME looks like
  4478.    it is the name of a file which resides either directly in a "system"
  4479.    include file directory, or within any subdirectory thereof, then the
  4480.    given file must be a "system" include file.  This function tells us
  4481.    if we should suppress pedantic errors/warnings for the given FILENAME.
  4482.  
  4483.    The value is 2 if the file is a C-language system header file
  4484.    for which C++ should (on most systems) assume `extern "C"'.  */
  4485.  
  4486. static int
  4487. is_system_include (filename)
  4488.     register char *filename;
  4489. {
  4490.   struct file_name_list *searchptr;
  4491.  
  4492.   for (searchptr = first_system_include; searchptr;
  4493.        searchptr = searchptr->next)
  4494.     if (searchptr->fname) {
  4495.       register char *sys_dir = searchptr->fname;
  4496.       register unsigned length = strlen (sys_dir);
  4497.  
  4498.       if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
  4499.     {
  4500.       if (searchptr->c_system_include_path)
  4501.         return 2;
  4502.       else
  4503.         return 1;
  4504.     }
  4505.     }
  4506.   return 0;
  4507. }
  4508.  
  4509. /* The file_name_map structure holds a mapping of file names for a
  4510.    particular directory.  This mapping is read from the file named
  4511.    FILE_NAME_MAP_FILE in that directory.  Such a file can be used to
  4512.    map filenames on a file system with severe filename restrictions,
  4513.    such as DOS.  The format of the file name map file is just a series
  4514.    of lines with two tokens on each line.  The first token is the name
  4515.    to map, and the second token is the actual name to use.  */
  4516.  
  4517. struct file_name_map
  4518. {
  4519.   struct file_name_map *map_next;
  4520.   char *map_from;
  4521.   char *map_to;
  4522. };
  4523.  
  4524. #define FILE_NAME_MAP_FILE "header.gcc"
  4525.  
  4526. /* Read a space delimited string of unlimited length from a stdio
  4527.    file.  */
  4528.  
  4529. static char *
  4530. read_filename_string (ch, f)
  4531.      int ch;
  4532.      FILE *f;
  4533. {
  4534.   char *alloc, *set;
  4535.   int len;
  4536.  
  4537.   len = 20;
  4538.   set = alloc = xmalloc (len + 1);
  4539.   if (! is_space[ch])
  4540.     {
  4541.       *set++ = ch;
  4542.       while ((ch = getc (f)) != EOF && ! is_space[ch])
  4543.     {
  4544.       if (set - alloc == len)
  4545.         {
  4546.           len *= 2;
  4547.           alloc = xrealloc (alloc, len + 1);
  4548.           set = alloc + len / 2;
  4549.         }
  4550.       *set++ = ch;
  4551.     }
  4552.     }
  4553.   *set = '\0';
  4554.   ungetc (ch, f);
  4555.   return alloc;
  4556. }
  4557.  
  4558. /* Read the file name map file for DIRNAME.  */
  4559.  
  4560. static struct file_name_map *
  4561. read_name_map (dirname)
  4562.      char *dirname;
  4563. {
  4564.   /* This structure holds a linked list of file name maps, one per
  4565.      directory.  */
  4566.   struct file_name_map_list
  4567.     {
  4568.       struct file_name_map_list *map_list_next;
  4569.       char *map_list_name;
  4570.       struct file_name_map *map_list_map;
  4571.     };
  4572.   static struct file_name_map_list *map_list;
  4573.   register struct file_name_map_list *map_list_ptr;
  4574.   char *name;
  4575.   FILE *f;
  4576.  
  4577.   for (map_list_ptr = map_list; map_list_ptr;
  4578.        map_list_ptr = map_list_ptr->map_list_next)
  4579.     if (! strcmp (map_list_ptr->map_list_name, dirname))
  4580.       return map_list_ptr->map_list_map;
  4581.  
  4582.   map_list_ptr = ((struct file_name_map_list *)
  4583.           xmalloc (sizeof (struct file_name_map_list)));
  4584.   map_list_ptr->map_list_name = savestring (dirname);
  4585.   map_list_ptr->map_list_map = NULL;
  4586.  
  4587.   name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
  4588.   strcpy (name, dirname);
  4589.   if (*dirname)
  4590.     strcat (name, "/");
  4591.   strcat (name, FILE_NAME_MAP_FILE);
  4592.   f = fopen (name, "r");
  4593.   if (!f)
  4594.     map_list_ptr->map_list_map = NULL;
  4595.   else
  4596.     {
  4597.       int ch;
  4598.       int dirlen = strlen (dirname);
  4599.  
  4600.       while ((ch = getc (f)) != EOF)
  4601.     {
  4602.       char *from, *to;
  4603.       struct file_name_map *ptr;
  4604.  
  4605.       if (is_space[ch])
  4606.         continue;
  4607.       from = read_filename_string (ch, f);
  4608.       while ((ch = getc (f)) != EOF && is_hor_space[ch])
  4609.         ;
  4610.       to = read_filename_string (ch, f);
  4611.  
  4612.       ptr = ((struct file_name_map *)
  4613.          xmalloc (sizeof (struct file_name_map)));
  4614.       ptr->map_from = from;
  4615.  
  4616.       /* Make the real filename absolute.  */
  4617.       if (*to == '/')
  4618.         ptr->map_to = to;
  4619.       else
  4620.         {
  4621.           ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
  4622.           strcpy (ptr->map_to, dirname);
  4623.           ptr->map_to[dirlen] = '/';
  4624.           strcpy (ptr->map_to + dirlen + 1, to);
  4625.           free (to);
  4626.         }          
  4627.  
  4628.       ptr->map_next = map_list_ptr->map_list_map;
  4629.       map_list_ptr->map_list_map = ptr;
  4630.  
  4631.       while ((ch = getc (f)) != '\n')
  4632.         if (ch == EOF)
  4633.           break;
  4634.     }
  4635.       fclose (f);
  4636.     }
  4637.   
  4638.   map_list_ptr->map_list_next = map_list;
  4639.   map_list = map_list_ptr;
  4640.  
  4641.   return map_list_ptr->map_list_map;
  4642. }  
  4643.  
  4644. /* Try to open include file FILENAME.  SEARCHPTR is the directory
  4645.    being tried from the include file search path.  This function maps
  4646.    filenames on file systems based on information read by
  4647.    read_name_map.  */
  4648.  
  4649. static int
  4650. open_include_file (filename, searchptr)
  4651.      char *filename;
  4652.      struct file_name_list *searchptr;
  4653. {
  4654.   register struct file_name_map *map;
  4655.   register char *from;
  4656.   char *p, *dir;
  4657.  
  4658.   if (searchptr && ! searchptr->got_name_map)
  4659.     {
  4660.       searchptr->name_map = read_name_map (searchptr->fname
  4661.                        ? searchptr->fname : ".");
  4662.       searchptr->got_name_map = 1;
  4663.     }
  4664.  
  4665.   /* First check the mapping for the directory we are using.  */
  4666.   if (searchptr && searchptr->name_map)
  4667.     {
  4668.       from = filename;
  4669.       if (searchptr->fname)
  4670.     from += strlen (searchptr->fname) + 1;
  4671.       for (map = searchptr->name_map; map; map = map->map_next)
  4672.     {
  4673.       if (! strcmp (map->map_from, from))
  4674.         {
  4675.           /* Found a match.  */
  4676.           return open (map->map_to, O_RDONLY, 0666);
  4677.         }
  4678.     }
  4679.     }
  4680.  
  4681.   /* Try to find a mapping file for the particular directory we are
  4682.      looking in.  Thus #include <sys/types.h> will look up sys/types.h
  4683.      in /usr/include/header.gcc and look up types.h in
  4684.      /usr/include/sys/header.gcc.  */
  4685.   p = rindex (filename, '/');
  4686.   if (! p)
  4687.     p = filename;
  4688.   if (searchptr
  4689.       && searchptr->fname
  4690.       && strlen (searchptr->fname) == p - filename
  4691.       && ! strncmp (searchptr->fname, filename, p - filename))
  4692.     {
  4693.       /* FILENAME is in SEARCHPTR, which we've already checked.  */
  4694.       return open (filename, O_RDONLY, 0666);
  4695.     }
  4696.  
  4697.   if (p == filename)
  4698.     {
  4699.       dir = ".";
  4700.       from = filename;
  4701.     }
  4702.   else
  4703.     {
  4704.       dir = (char *) alloca (p - filename + 1);
  4705.       bcopy (filename, dir, p - filename);
  4706.       dir[p - filename] = '\0';
  4707.       from = p + 1;
  4708.     }
  4709.   for (map = read_name_map (dir); map; map = map->map_next)
  4710.     if (! strcmp (map->map_from, from))
  4711.       return open (map->map_to, O_RDONLY, 0666);
  4712.  
  4713.   return open (filename, O_RDONLY, 0666);
  4714. }
  4715.  
  4716. /* Process the contents of include file FNAME, already open on descriptor F,
  4717.    with output to OP.
  4718.    SYSTEM_HEADER_P is 1 if this file resides in any one of the known
  4719.    "system" include directories (as decided by the `is_system_include'
  4720.    function above).
  4721.    DIRPTR is the link in the dir path through which this file was found,
  4722.    or 0 if the file name was absolute.  */
  4723.  
  4724. static void
  4725. finclude (f, fname, op, system_header_p, dirptr)
  4726.      int f;
  4727.      char *fname;
  4728.      FILE_BUF *op;
  4729.      int system_header_p;
  4730.      struct file_name_list *dirptr;
  4731. {
  4732.   int st_mode;
  4733.   long st_size;
  4734.   long i;
  4735.   FILE_BUF *fp;            /* For input stack frame */
  4736.   int missing_newline = 0;
  4737.  
  4738.   CHECK_DEPTH (return;);
  4739.  
  4740.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  4741.     {
  4742.       perror_with_name (fname);
  4743.       close (f);
  4744.       return;
  4745.     }
  4746.  
  4747.   fp = &instack[indepth + 1];
  4748.   bzero ((char *) fp, sizeof (FILE_BUF));
  4749.   fp->nominal_fname = fp->fname = fname;
  4750.   fp->length = 0;
  4751.   fp->lineno = 1;
  4752.   fp->if_stack = if_stack;
  4753.   fp->system_header_p = system_header_p;
  4754.   fp->dir = dirptr;
  4755.  
  4756.   if (S_ISREG (st_mode)) {
  4757.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  4758.     fp->bufp = fp->buf;
  4759.  
  4760.     /* Read the file contents, knowing that st_size is an upper bound
  4761.        on the number of bytes we can read.  */
  4762.     fp->length = safe_read (f, fp->buf, st_size);
  4763.     if (fp->length < 0) goto nope;
  4764.   }
  4765.   else if (S_ISDIR (st_mode)) {
  4766.     error ("directory `%s' specified in #include", fname);
  4767.     close (f);
  4768.     return;
  4769.   } else {
  4770.     /* Cannot count its file size before reading.
  4771.        First read the entire file into heap and
  4772.        copy them into buffer on stack. */
  4773.  
  4774.     int bsize = 2000;
  4775.  
  4776.     st_size = 0;
  4777.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  4778.  
  4779.     for (;;) {
  4780.       i = safe_read (f, fp->buf + st_size, bsize - st_size);
  4781.       if (i < 0)
  4782.     goto nope;      /* error! */
  4783.       st_size += i;
  4784.       if (st_size != bsize)
  4785.     break;    /* End of file */
  4786.       bsize *= 2;
  4787.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  4788.     }
  4789.     fp->bufp = fp->buf;
  4790.     fp->length = st_size;
  4791.   }
  4792.  
  4793.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  4794.       /* Backslash-newline at end is not good enough.  */
  4795.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  4796.     fp->buf[fp->length++] = '\n';
  4797.     missing_newline = 1;
  4798.   }
  4799.   fp->buf[fp->length] = '\0';
  4800.  
  4801.   /* Close descriptor now, so nesting does not use lots of descriptors.  */
  4802.   close (f);
  4803.  
  4804.   /* Must do this before calling trigraph_pcp, so that the correct file name
  4805.      will be printed in warning messages.  */
  4806.  
  4807.   indepth++;
  4808.   input_file_stack_tick++;
  4809.  
  4810.   if (!no_trigraphs)
  4811.     trigraph_pcp (fp);
  4812.  
  4813.   output_line_command (fp, op, 0, enter_file);
  4814.   rescan (op, 0);
  4815.  
  4816.   if (missing_newline)
  4817.     fp->lineno--;
  4818.  
  4819.   if (pedantic && missing_newline)
  4820.     pedwarn ("file does not end in newline");
  4821.  
  4822.   indepth--;
  4823.   input_file_stack_tick++;
  4824.   output_line_command (&instack[indepth], op, 0, leave_file);
  4825.   free (fp->buf);
  4826.   return;
  4827.  
  4828.  nope:
  4829.  
  4830.   perror_with_name (fname);
  4831.   close (f);
  4832.   free (fp->buf);
  4833. }
  4834.  
  4835. /* Record that inclusion of the file named FILE
  4836.    should be controlled by the macro named MACRO_NAME.
  4837.    This means that trying to include the file again
  4838.    will do something if that macro is defined.  */
  4839.  
  4840. static void
  4841. record_control_macro (file, macro_name)
  4842.      char *file;
  4843.      U_CHAR *macro_name;
  4844. {
  4845.   struct file_name_list *new;
  4846.  
  4847.   for (new = all_include_files; new; new = new->next) {
  4848.     if (!strcmp (new->fname, file)) {
  4849.       new->control_macro = macro_name;
  4850.       return;
  4851.     }
  4852.   }
  4853.  
  4854.   /* If the file is not in all_include_files, something's wrong.  */
  4855.   abort ();
  4856. }
  4857.  
  4858. /* Maintain and search list of included files, for #import.  */
  4859.  
  4860. #define IMPORT_HASH_SIZE 31
  4861.  
  4862. struct import_file {
  4863.   char *name;
  4864.   ino_t inode;
  4865.   dev_t dev;
  4866.   struct import_file *next;
  4867. };
  4868.  
  4869. /* Hash table of files already included with #include or #import.  */
  4870.  
  4871. static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
  4872.  
  4873. /* Hash a file name for import_hash_table.  */
  4874.  
  4875. static int 
  4876. import_hash (f)
  4877.      char *f;
  4878. {
  4879.   int val = 0;
  4880.  
  4881.   while (*f) val += *f++;
  4882.   return (val%IMPORT_HASH_SIZE);
  4883. }
  4884.  
  4885. /* Search for file FILENAME in import_hash_table.
  4886.    Return -2 if found, either a matching name or a matching inode.
  4887.    Otherwise, open the file and return a file descriptor if successful
  4888.    or -1 if unsuccessful.  */
  4889.  
  4890. static int
  4891. lookup_import (filename, searchptr)
  4892.      char *filename;
  4893.      struct file_name_list *searchptr;
  4894. {
  4895.   struct import_file *i;
  4896.   int h;
  4897.   int hashval;
  4898.   struct stat sb;
  4899.   int fd;
  4900.  
  4901.   hashval = import_hash (filename);
  4902.  
  4903.   /* Attempt to find file in list of already included files */
  4904.   i = import_hash_table[hashval];
  4905.  
  4906.   while (i) {
  4907.     if (!strcmp (filename, i->name))
  4908.       return -2;        /* return found */
  4909.     i = i->next;
  4910.   }
  4911.   /* Open it and try a match on inode/dev */
  4912.   fd = open_include_file (filename, searchptr);
  4913.   if (fd < 0)
  4914.     return fd;
  4915.   fstat (fd, &sb);
  4916.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  4917.     i = import_hash_table[h];
  4918.     while (i) {
  4919.       /* Compare the inode and the device.
  4920.      Supposedly on some systems the inode is not a scalar.  */
  4921.       if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
  4922.       && i->dev == sb.st_dev) {
  4923.         close (fd);
  4924.         return -2;        /* return found */
  4925.       }
  4926.       i = i->next;
  4927.     }
  4928.   }
  4929.   return fd;            /* Not found, return open file */
  4930. }
  4931.  
  4932. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  4933.  
  4934. static void
  4935. add_import (fd, fname)
  4936.      int fd;
  4937.      char *fname;
  4938. {
  4939.   struct import_file *i;
  4940.   int hashval;
  4941.   struct stat sb;
  4942.  
  4943.   hashval = import_hash (fname);
  4944.   fstat (fd, &sb);
  4945.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  4946.   i->name = (char *)xmalloc (strlen (fname)+1);
  4947.   strcpy (i->name, fname);
  4948.   bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
  4949.   i->dev = sb.st_dev;
  4950.   i->next = import_hash_table[hashval];
  4951.   import_hash_table[hashval] = i;
  4952. }
  4953.  
  4954. /* Load the specified precompiled header into core, and verify its
  4955.    preconditions.  PCF indicates the file descriptor to read, which must
  4956.    be a regular file.  FNAME indicates the file name of the original 
  4957.    header.  *LIMIT will be set to an address one past the end of the file.
  4958.    If the preconditions of the file are not satisfied, the buffer is 
  4959.    freed and we return 0.  If the preconditions are satisfied, return
  4960.    the address of the buffer following the preconditions.  The buffer, in
  4961.    this case, should never be freed because various pieces of it will
  4962.    be referred to until all precompiled strings are output at the end of
  4963.    the run.
  4964. */
  4965. static char *
  4966. check_precompiled (pcf, fname, limit)
  4967.      int pcf;
  4968.      char *fname;
  4969.      char **limit;
  4970. {
  4971.   int st_mode;
  4972.   long st_size;
  4973.   int length = 0;
  4974.   char *buf;
  4975.   char *cp;
  4976.  
  4977.   if (pcp_outfile)
  4978.     return 0;
  4979.   
  4980.   if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
  4981.     return 0;
  4982.  
  4983.   if (S_ISREG (st_mode))
  4984.     {
  4985.       buf = xmalloc (st_size + 2);
  4986.       length = safe_read (pcf, buf, st_size);
  4987.       if (length < 0)
  4988.     goto nope;
  4989.     }
  4990.   else
  4991.     abort ();
  4992.     
  4993.   if (length > 0 && buf[length-1] != '\n')
  4994.     buf[length++] = '\n';
  4995.   buf[length] = '\0';
  4996.   
  4997.   *limit = buf + length;
  4998.  
  4999.   /* File is in core.  Check the preconditions. */
  5000.   if (!check_preconditions (buf))
  5001.     goto nope;
  5002.   for (cp = buf; *cp; cp++)
  5003.     ;
  5004. #ifdef DEBUG_PCP
  5005.   fprintf (stderr, "Using preinclude %s\n", fname);
  5006. #endif
  5007.   return cp + 1;
  5008.  
  5009.  nope:
  5010. #ifdef DEBUG_PCP
  5011.   fprintf (stderr, "Cannot use preinclude %s\n", fname);
  5012. #endif
  5013.   free (buf);
  5014.   return 0;
  5015. }
  5016.  
  5017. /* PREC (null terminated) points to the preconditions of a
  5018.    precompiled header.  These are a series of #define and #undef
  5019.    lines which must match the current contents of the hash
  5020.    table.  */
  5021. static int 
  5022. check_preconditions (prec)
  5023.      char *prec;
  5024. {
  5025.   MACRODEF mdef;
  5026.   char *lineend;
  5027.   
  5028.   while (*prec) {
  5029.     lineend = (char *) index (prec, '\n');
  5030.     
  5031.     if (*prec++ != '#') {
  5032.       error ("Bad format encountered while reading precompiled file");
  5033.       return 0;
  5034.     }
  5035.     if (!strncmp (prec, "define", 6)) {
  5036.       HASHNODE *hp;
  5037.       
  5038.       prec += 6;
  5039.       mdef = create_definition (prec, lineend, NULL_PTR);
  5040.  
  5041.       if (mdef.defn == 0)
  5042.     abort ();
  5043.       
  5044.       if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
  5045.       || (hp->type != T_MACRO && hp->type != T_CONST)
  5046.       || (hp->type == T_MACRO
  5047.           && !compare_defs (mdef.defn, hp->value.defn)
  5048.           && (mdef.defn->length != 2
  5049.           || mdef.defn->expansion[0] != '\n'
  5050.           || mdef.defn->expansion[1] != ' ')))
  5051.     return 0;
  5052.     } else if (!strncmp (prec, "undef", 5)) {
  5053.       char *name;
  5054.       int len;
  5055.       
  5056.       prec += 5;
  5057.       while (is_hor_space[(U_CHAR) *prec])
  5058.     prec++;
  5059.       name = prec;
  5060.       while (is_idchar[(U_CHAR) *prec])
  5061.     prec++;
  5062.       len = prec - name;
  5063.       
  5064.       if (lookup (name, len, -1))
  5065.     return 0;
  5066.     } else {
  5067.       error ("Bad format encountered while reading precompiled file");
  5068.       return 0;
  5069.     }
  5070.     prec = lineend + 1;
  5071.   }
  5072.   /* They all passed successfully */
  5073.   return 1;
  5074. }
  5075.  
  5076. /* Process the main body of a precompiled file.  BUF points to the
  5077.    string section of the file, following the preconditions.  LIMIT is one
  5078.    character past the end.  NAME is the name of the file being read
  5079.    in.  OP is the main output buffer */
  5080. static void
  5081. pcfinclude (buf, limit, name, op)
  5082.      U_CHAR *buf, *limit, *name;
  5083.      FILE_BUF *op;
  5084. {
  5085.   FILE_BUF tmpbuf;
  5086.   int nstrings;
  5087.   U_CHAR *cp = buf;
  5088.  
  5089.   /* First in the file comes 4 bytes indicating the number of strings, */
  5090.   /* in network byte order. (MSB first).  */
  5091.   nstrings = *cp++;
  5092.   nstrings = (nstrings << 8) | *cp++;
  5093.   nstrings = (nstrings << 8) | *cp++;
  5094.   nstrings = (nstrings << 8) | *cp++;
  5095.   
  5096.   /* Looping over each string... */
  5097.   while (nstrings--) {
  5098.     U_CHAR *string_start;
  5099.     U_CHAR *endofthiskey;
  5100.     STRINGDEF *str;
  5101.     int nkeys;
  5102.     
  5103.     /* Each string starts with a STRINGDEF structure (str), followed */
  5104.     /* by the text of the string (string_start) */
  5105.  
  5106.     /* First skip to a longword boundary */
  5107.     /* ??? Why a 4-byte boundary?  On all machines? */
  5108.     /* NOTE: This works correctly even if HOST_WIDE_INT
  5109.        is narrower than a pointer.
  5110.        Do not try risky measures here to get another type to use!
  5111.        Do not include stddef.h--it will fail!  */
  5112.     if ((HOST_WIDE_INT) cp & 3)
  5113.       cp += 4 - ((HOST_WIDE_INT) cp & 3);
  5114.     
  5115.     /* Now get the string. */
  5116.     str = (STRINGDEF *) cp;
  5117.     string_start = cp += sizeof (STRINGDEF);
  5118.     
  5119.     for (; *cp; cp++)        /* skip the string */
  5120.       ;
  5121.     
  5122.     /* We need to macro expand the string here to ensure that the
  5123.        proper definition environment is in place.  If it were only
  5124.        expanded when we find out it is needed, macros necessary for
  5125.        its proper expansion might have had their definitions changed. */
  5126.     tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
  5127.     /* Lineno is already set in the precompiled file */
  5128.     str->contents = tmpbuf.buf;
  5129.     str->len = tmpbuf.length;
  5130.     str->writeflag = 0;
  5131.     str->filename = name;
  5132.     str->output_mark = outbuf.bufp - outbuf.buf;
  5133.     
  5134.     str->chain = 0;
  5135.     *stringlist_tailp = str;
  5136.     stringlist_tailp = &str->chain;
  5137.     
  5138.     /* Next comes a fourbyte number indicating the number of keys */
  5139.     /* for this string. */
  5140.     nkeys = *cp++;
  5141.     nkeys = (nkeys << 8) | *cp++;
  5142.     nkeys = (nkeys << 8) | *cp++;
  5143.     nkeys = (nkeys << 8) | *cp++;
  5144.  
  5145.     /* If this number is -1, then the string is mandatory. */
  5146.     if (nkeys == -1)
  5147.       str->writeflag = 1;
  5148.     else
  5149.       /* Otherwise, for each key, */
  5150.       for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
  5151.     KEYDEF *kp = (KEYDEF *) cp;
  5152.     HASHNODE *hp;
  5153.     
  5154.     /* It starts with a KEYDEF structure */
  5155.     cp += sizeof (KEYDEF);
  5156.     
  5157.     /* Find the end of the key.  At the end of this for loop we
  5158.        advance CP to the start of the next key using this variable. */
  5159.     endofthiskey = cp + strlen (cp);
  5160.     kp->str = str;
  5161.     
  5162.     /* Expand the key, and enter it into the hash table. */
  5163.     tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
  5164.     tmpbuf.bufp = tmpbuf.buf;
  5165.     
  5166.     while (is_hor_space[*tmpbuf.bufp])
  5167.       tmpbuf.bufp++;
  5168.     if (!is_idstart[*tmpbuf.bufp]
  5169.         || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
  5170.       str->writeflag = 1;
  5171.       continue;
  5172.     }
  5173.         
  5174.     hp = lookup (tmpbuf.bufp, -1, -1);
  5175.     if (hp == NULL) {
  5176.       kp->chain = 0;
  5177.       install (tmpbuf.bufp, -1, T_PCSTRING, 0, (char *) kp, -1);
  5178.     }
  5179.     else if (hp->type == T_PCSTRING) {
  5180.       kp->chain = hp->value.keydef;
  5181.       hp->value.keydef = kp;
  5182.     }
  5183.     else
  5184.       str->writeflag = 1;
  5185.       }
  5186.   }
  5187.   /* This output_line_command serves to switch us back to the current
  5188.      input file in case some of these strings get output (which will 
  5189.      result in line commands for the header file being output). */
  5190.   output_line_command (&instack[indepth], op, 0, enter_file);
  5191. }
  5192.  
  5193. /* Called from rescan when it hits a key for strings.  Mark them all */
  5194.  /* used and clean up. */
  5195. static void
  5196. pcstring_used (hp)
  5197.      HASHNODE *hp;
  5198. {
  5199.   KEYDEF *kp;
  5200.   
  5201.   for (kp = hp->value.keydef; kp; kp = kp->chain)
  5202.     kp->str->writeflag = 1;
  5203.   delete_macro (hp);
  5204. }
  5205.  
  5206. /* Write the output, interspersing precompiled strings in their */
  5207.  /* appropriate places. */
  5208. static void
  5209. write_output ()
  5210. {
  5211.   STRINGDEF *next_string;
  5212.   U_CHAR *cur_buf_loc;
  5213.   int line_command_len = 80;
  5214.   char *line_command = xmalloc (line_command_len);
  5215.   int len;
  5216.  
  5217.   /* In each run through the loop, either cur_buf_loc == */
  5218.   /* next_string_loc, in which case we print a series of strings, or */
  5219.   /* it is less than next_string_loc, in which case we write some of */
  5220.   /* the buffer. */
  5221.   cur_buf_loc = outbuf.buf; 
  5222.   next_string = stringlist;
  5223.   
  5224.   while (cur_buf_loc < outbuf.bufp || next_string) {
  5225.     if (next_string
  5226.     && cur_buf_loc - outbuf.buf == next_string->output_mark) {
  5227.       if (next_string->writeflag) {
  5228.     len = 4 * strlen (next_string->filename) + 32;
  5229.     while (len > line_command_len)
  5230.       line_command = xrealloc (line_command, 
  5231.                    line_command_len *= 2);
  5232.     sprintf (line_command, "\n# %d ", next_string->lineno);
  5233.     strcpy (quote_string (line_command + strlen (line_command),
  5234.                       next_string->filename),
  5235.         "\n");
  5236.     safe_write (fileno (stdout), line_command, strlen (line_command));
  5237.     safe_write (fileno (stdout), next_string->contents, next_string->len);
  5238.       }          
  5239.       next_string = next_string->chain;
  5240.     }
  5241.     else {
  5242.       len = (next_string
  5243.          ? (next_string->output_mark 
  5244.         - (cur_buf_loc - outbuf.buf))
  5245.          : outbuf.bufp - cur_buf_loc);
  5246.       
  5247.       safe_write (fileno (stdout), cur_buf_loc, len);
  5248.       cur_buf_loc += len;
  5249.     }
  5250.   }
  5251.   free (line_command);
  5252. }
  5253.  
  5254. /* Pass a directive through to the output file.
  5255.    BUF points to the contents of the directive, as a contiguous string.
  5256.    LIMIT points to the first character past the end of the directive.
  5257.    KEYWORD is the keyword-table entry for the directive.  */
  5258.  
  5259. static void
  5260. pass_thru_directive (buf, limit, op, keyword)
  5261.      U_CHAR *buf, *limit;
  5262.      FILE_BUF *op;
  5263.      struct directive *keyword;
  5264. {
  5265.   register unsigned keyword_length = keyword->length;
  5266.  
  5267.   check_expand (op, 1 + keyword_length + (limit - buf));
  5268.   *op->bufp++ = '#';
  5269.   bcopy (keyword->name, (char *) op->bufp, keyword_length);
  5270.   op->bufp += keyword_length;
  5271.   if (limit != buf && buf[0] != ' ')
  5272.     *op->bufp++ = ' ';
  5273.   bcopy ((char *) buf, (char *) op->bufp, limit - buf);
  5274.   op->bufp += (limit - buf);
  5275. #if 0
  5276.   *op->bufp++ = '\n';
  5277.   /* Count the line we have just made in the output,
  5278.      to get in sync properly.  */
  5279.   op->lineno++;
  5280. #endif
  5281. }
  5282.  
  5283. /* The arglist structure is built by do_define to tell
  5284.    collect_definition where the argument names begin.  That
  5285.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  5286.    would contain pointers to the strings x, y, and z.
  5287.    Collect_definition would then build a DEFINITION node,
  5288.    with reflist nodes pointing to the places x, y, and z had
  5289.    appeared.  So the arglist is just convenience data passed
  5290.    between these two routines.  It is not kept around after
  5291.    the current #define has been processed and entered into the
  5292.    hash table. */
  5293.  
  5294. struct arglist {
  5295.   struct arglist *next;
  5296.   U_CHAR *name;
  5297.   int length;
  5298.   int argno;
  5299.   char rest_args;
  5300. };
  5301.  
  5302. /* Create a DEFINITION node from a #define directive.  Arguments are 
  5303.    as for do_define. */
  5304. static MACRODEF
  5305. create_definition (buf, limit, op)
  5306.      U_CHAR *buf, *limit;
  5307.      FILE_BUF *op;
  5308. {
  5309.   U_CHAR *bp;            /* temp ptr into input buffer */
  5310.   U_CHAR *symname;        /* remember where symbol name starts */
  5311.   int sym_length;        /* and how long it is */
  5312.   int line = instack[indepth].lineno;
  5313.   char *file = instack[indepth].nominal_fname;
  5314.   int rest_args = 0;
  5315.  
  5316.   DEFINITION *defn;
  5317.   int arglengths = 0;        /* Accumulate lengths of arg names
  5318.                    plus number of args.  */
  5319.   MACRODEF mdef;
  5320.  
  5321.   bp = buf;
  5322.  
  5323.   while (is_hor_space[*bp])
  5324.     bp++;
  5325.  
  5326.   symname = bp;            /* remember where it starts */
  5327.   sym_length = check_macro_name (bp, "macro");
  5328.   bp += sym_length;
  5329.  
  5330.   /* Lossage will occur if identifiers or control keywords are broken
  5331.      across lines using backslash.  This is not the right place to take
  5332.      care of that. */
  5333.  
  5334.   if (*bp == '(') {
  5335.     struct arglist *arg_ptrs = NULL;
  5336.     int argno = 0;
  5337.  
  5338.     bp++;            /* skip '(' */
  5339.     SKIP_WHITE_SPACE (bp);
  5340.  
  5341.     /* Loop over macro argument names.  */
  5342.     while (*bp != ')') {
  5343.       struct arglist *temp;
  5344.  
  5345.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  5346.       temp->name = bp;
  5347.       temp->next = arg_ptrs;
  5348.       temp->argno = argno++;
  5349.       temp->rest_args = 0;
  5350.       arg_ptrs = temp;
  5351.  
  5352.       if (rest_args)
  5353.     pedwarn ("another parameter follows `%s'",
  5354.          rest_extension);
  5355.  
  5356.       if (!is_idstart[*bp])
  5357.     pedwarn ("invalid character in macro parameter name");
  5358.       
  5359.       /* Find the end of the arg name.  */
  5360.       while (is_idchar[*bp]) {
  5361.     bp++;
  5362.     /* do we have a "special" rest-args extension here? */
  5363.     if (limit - bp > REST_EXTENSION_LENGTH &&
  5364.         strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
  5365.       rest_args = 1;
  5366.       temp->rest_args = 1;
  5367.       break;
  5368.     }
  5369.       }
  5370.       temp->length = bp - temp->name;
  5371.       if (rest_args == 1)
  5372.     bp += REST_EXTENSION_LENGTH;
  5373.       arglengths += temp->length + 2;
  5374.       SKIP_WHITE_SPACE (bp);
  5375.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  5376.     error ("badly punctuated parameter list in `#define'");
  5377.     goto nope;
  5378.       }
  5379.       if (*bp == ',') {
  5380.     bp++;
  5381.     SKIP_WHITE_SPACE (bp);
  5382.       }
  5383.       if (bp >= limit) {
  5384.     error ("unterminated parameter list in `#define'");
  5385.     goto nope;
  5386.       }
  5387.       {
  5388.     struct arglist *otemp;
  5389.  
  5390.     for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
  5391.       if (temp->length == otemp->length &&
  5392.         strncmp (temp->name, otemp->name, temp->length) == 0) {
  5393.           U_CHAR *name;
  5394.  
  5395.           name = (U_CHAR *) alloca (temp->length + 1);
  5396.           (void) strncpy (name, temp->name, temp->length);
  5397.           name[temp->length] = '\0';
  5398.           error ("duplicate argument name `%s' in `#define'", name);
  5399.           goto nope;
  5400.       }
  5401.       }
  5402.     }
  5403.  
  5404.     ++bp;            /* skip paren */
  5405.     /* Skip spaces and tabs if any.  */
  5406.     while (bp < limit && (*bp == ' ' || *bp == '\t'))
  5407.       ++bp;
  5408.     /* now everything from bp before limit is the definition. */
  5409.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  5410.     defn->rest_args = rest_args;
  5411.  
  5412.     /* Now set defn->args.argnames to the result of concatenating
  5413.        the argument names in reverse order
  5414.        with comma-space between them.  */
  5415.     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
  5416.     {
  5417.       struct arglist *temp;
  5418.       int i = 0;
  5419.       for (temp = arg_ptrs; temp; temp = temp->next) {
  5420.     bcopy (temp->name, &defn->args.argnames[i], temp->length);
  5421.     i += temp->length;
  5422.     if (temp->next != 0) {
  5423.       defn->args.argnames[i++] = ',';
  5424.       defn->args.argnames[i++] = ' ';
  5425.     }
  5426.       }
  5427.       defn->args.argnames[i] = 0;
  5428.     }
  5429.   } else {
  5430.     /* Simple expansion or empty definition.  */
  5431.  
  5432.     if (bp < limit)
  5433.       {
  5434.     switch (*bp)
  5435.       {
  5436.         case '\t': case ' ': case '\r':
  5437.           /* Skip spaces and tabs.  */
  5438.           while (++bp < limit && (*bp == ' ' || *bp == '\t' || *bp == '\r'))
  5439.         continue;
  5440.           break;
  5441.  
  5442.         case '!':  case '"':  case '#':  case '%':  case '&':  case '\'':
  5443.         case ')':  case '*':  case '+':  case ',':  case '-':  case '.':
  5444.         case '/':  case ':':  case ';':  case '<':  case '=':  case '>':
  5445.         case '?':  case '[':  case '\\': case ']':  case '^':  case '{':
  5446.         case '|':  case '}':  case '~':
  5447.           warning ("missing white space after `#define %.*s'",
  5448.                sym_length, symname);
  5449.           break;
  5450.  
  5451.         default:
  5452.           pedwarn ("missing white space after `#define %.*s'",
  5453.                sym_length, symname);
  5454.           break;
  5455.       }
  5456.       }
  5457.     /* Now everything from bp before limit is the definition. */
  5458.     defn = collect_expansion (bp, limit, -1, NULL_PTR);
  5459.     defn->args.argnames = (U_CHAR *) "";
  5460.   }
  5461.  
  5462.   defn->line = line;
  5463.   defn->file = file;
  5464.  
  5465.   /* OP is null if this is a predefinition */
  5466.   defn->predefined = !op;
  5467.   mdef.defn = defn;
  5468.   mdef.symnam = symname;
  5469.   mdef.symlen = sym_length;
  5470.  
  5471.   return mdef;
  5472.  
  5473.  nope:
  5474.   mdef.defn = 0;
  5475.   return mdef;
  5476. }
  5477.  
  5478. /* Process a #define command.
  5479. BUF points to the contents of the #define command, as a contiguous string.
  5480. LIMIT points to the first character past the end of the definition.
  5481. KEYWORD is the keyword-table entry for #define.  */
  5482.  
  5483. static int
  5484. do_define (buf, limit, op, keyword)
  5485.      U_CHAR *buf, *limit;
  5486.      FILE_BUF *op;
  5487.      struct directive *keyword;
  5488. {
  5489.   int hashcode;
  5490.   MACRODEF mdef;
  5491.  
  5492.   /* If this is a precompiler run (with -pcp) pass thru #define commands.  */
  5493.   if (pcp_outfile && op)
  5494.     pass_thru_directive (buf, limit, op, keyword);
  5495.  
  5496.   mdef = create_definition (buf, limit, op);
  5497.   if (mdef.defn == 0)
  5498.     goto nope;
  5499.  
  5500.   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
  5501.  
  5502.   {
  5503.     HASHNODE *hp;
  5504.     if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
  5505.       int ok = 0;
  5506.       /* Redefining a precompiled key is ok.  */
  5507.       if (hp->type == T_PCSTRING)
  5508.     ok = 1;
  5509.       /* Redefining a macro is ok if the definitions are the same.  */
  5510.       else if (hp->type == T_MACRO)
  5511.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  5512.       /* Redefining a constant is ok with -D.  */
  5513.       else if (hp->type == T_CONST)
  5514.         ok = ! done_initializing;
  5515.       /* Print the warning if it's not ok.  */
  5516.       if (!ok) {
  5517.     U_CHAR *msg;        /* what pain... */
  5518.  
  5519.         /* If we are passing through #define and #undef directives, do
  5520.        that for this re-definition now.  */
  5521.         if (debug_output && op)
  5522.       pass_thru_directive (buf, limit, op, keyword);
  5523.  
  5524.     msg = (U_CHAR *) alloca (mdef.symlen + 22);
  5525.     *msg = '`';
  5526.     bcopy ((char *) mdef.symnam, (char *) (msg + 1), mdef.symlen);
  5527.     strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
  5528.     pedwarn (msg);
  5529.     if (hp->type == T_MACRO)
  5530.       pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
  5531.                       "this is the location of the previous definition");
  5532.       }
  5533.       /* Replace the old definition.  */
  5534.       hp->type = T_MACRO;
  5535.       hp->value.defn = mdef.defn;
  5536.     } else {
  5537.       /* If we are passing through #define and #undef directives, do
  5538.      that for this new definition now.  */
  5539.       if (debug_output && op)
  5540.     pass_thru_directive (buf, limit, op, keyword);
  5541.       install (mdef.symnam, mdef.symlen, T_MACRO, 0,
  5542.            (char *) mdef.defn, hashcode);
  5543.     }
  5544.   }
  5545.  
  5546.   return 0;
  5547.  
  5548. nope:
  5549.  
  5550.   return 1;
  5551. }
  5552.  
  5553. /* Check a purported macro name SYMNAME, and yield its length.
  5554.    USAGE is the kind of name this is intended for.  */
  5555.  
  5556. static int
  5557. check_macro_name (symname, usage)
  5558.      U_CHAR *symname;
  5559.      char *usage;
  5560. {
  5561.   U_CHAR *p;
  5562.   int sym_length;
  5563.  
  5564.   for (p = symname; is_idchar[*p]; p++)
  5565.     ;
  5566.   sym_length = p - symname;
  5567.   if (sym_length == 0)
  5568.     error ("invalid %s name", usage);
  5569.   else if (!is_idstart[*symname]) {
  5570.     U_CHAR *msg;            /* what pain... */
  5571.     msg = (U_CHAR *) alloca (sym_length + 1);
  5572.     bcopy ((char *) symname, (char *) msg, sym_length);
  5573.     msg[sym_length] = 0;
  5574.     error ("invalid %s name `%s'", usage, msg);
  5575.   } else {
  5576.     if (! strncmp (symname, "defined", 7) && sym_length == 7)
  5577.       error ("invalid %s name `defined'", usage);
  5578.   }
  5579.   return sym_length;
  5580. }
  5581.  
  5582. /*
  5583.  * return zero if two DEFINITIONs are isomorphic
  5584.  */
  5585. static int
  5586. compare_defs (d1, d2)
  5587.      DEFINITION *d1, *d2;
  5588. {
  5589.   register struct reflist *a1, *a2;
  5590.   register U_CHAR *p1 = d1->expansion;
  5591.   register U_CHAR *p2 = d2->expansion;
  5592.   int first = 1;
  5593.  
  5594.   if (d1->nargs != d2->nargs)
  5595.     return 1;
  5596.   if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
  5597.     return 1;
  5598.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  5599.        a1 = a1->next, a2 = a2->next) {
  5600.     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  5601.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  5602.     || a1->argno != a2->argno
  5603.     || a1->stringify != a2->stringify
  5604.     || a1->raw_before != a2->raw_before
  5605.     || a1->raw_after != a2->raw_after)
  5606.       return 1;
  5607.     first = 0;
  5608.     p1 += a1->nchars;
  5609.     p2 += a2->nchars;
  5610.   }
  5611.   if (a1 != a2)
  5612.     return 1;
  5613.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  5614.              p2, d2->length - (p2 - d2->expansion), 1))
  5615.     return 1;
  5616.   return 0;
  5617. }
  5618.  
  5619. /* Return 1 if two parts of two macro definitions are effectively different.
  5620.    One of the parts starts at BEG1 and has LEN1 chars;
  5621.    the other has LEN2 chars at BEG2.
  5622.    Any sequence of whitespace matches any other sequence of whitespace.
  5623.    FIRST means these parts are the first of a macro definition;
  5624.     so ignore leading whitespace entirely.
  5625.    LAST means these parts are the last of a macro definition;
  5626.     so ignore trailing whitespace entirely.  */
  5627.  
  5628. static int
  5629. comp_def_part (first, beg1, len1, beg2, len2, last)
  5630.      int first;
  5631.      U_CHAR *beg1, *beg2;
  5632.      int len1, len2;
  5633.      int last;
  5634. {
  5635.   register U_CHAR *end1 = beg1 + len1;
  5636.   register U_CHAR *end2 = beg2 + len2;
  5637.   if (first) {
  5638.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  5639.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  5640.   }
  5641.   if (last) {
  5642.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  5643.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  5644.   }
  5645.   while (beg1 != end1 && beg2 != end2) {
  5646.     if (is_space[*beg1] && is_space[*beg2]) {
  5647.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  5648.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  5649.     } else if (*beg1 == *beg2) {
  5650.       beg1++; beg2++;
  5651.     } else break;
  5652.   }
  5653.   return (beg1 != end1) || (beg2 != end2);
  5654. }
  5655.  
  5656. /* Read a replacement list for a macro with parameters.
  5657.    Build the DEFINITION structure.
  5658.    Reads characters of text starting at BUF until END.
  5659.    ARGLIST specifies the formal parameters to look for
  5660.    in the text of the definition; NARGS is the number of args
  5661.    in that list, or -1 for a macro name that wants no argument list.
  5662.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  5663.    and NAMELEN is its length in characters.
  5664.    
  5665. Note that comments and backslash-newlines have already been deleted
  5666. from the argument.  */
  5667.  
  5668. /* Leading and trailing Space, Tab, etc. are converted to markers
  5669.    Newline Space, Newline Tab, etc.
  5670.    Newline Space makes a space in the final output
  5671.    but is discarded if stringified.  (Newline Tab is similar but
  5672.    makes a Tab instead.)
  5673.  
  5674.    If there is no trailing whitespace, a Newline Space is added at the end
  5675.    to prevent concatenation that would be contrary to the standard.  */
  5676.  
  5677. static DEFINITION *
  5678. collect_expansion (buf, end, nargs, arglist)
  5679.      U_CHAR *buf, *end;
  5680.      int nargs;
  5681.      struct arglist *arglist;
  5682. {
  5683.   DEFINITION *defn;
  5684.   register U_CHAR *p, *limit, *lastp, *exp_p;
  5685.   struct reflist *endpat = NULL;
  5686.   /* Pointer to first nonspace after last ## seen.  */
  5687.   U_CHAR *concat = 0;
  5688.   /* Pointer to first nonspace after last single-# seen.  */
  5689.   U_CHAR *stringify = 0;
  5690.   int maxsize;
  5691.   int expected_delimiter = '\0';
  5692.  
  5693.   /* Scan thru the replacement list, ignoring comments and quoted
  5694.      strings, picking up on the macro calls.  It does a linear search
  5695.      thru the arg list on every potential symbol.  Profiling might say
  5696.      that something smarter should happen. */
  5697.  
  5698.   if (end < buf)
  5699.     abort ();
  5700.  
  5701.   /* Find the beginning of the trailing whitespace.  */
  5702.   /* Find end of leading whitespace.  */
  5703.   limit = end;
  5704.   p = buf;
  5705.   while (p < limit && is_space[limit[-1]]) limit--;
  5706.   while (p < limit && is_space[*p]) p++;
  5707.  
  5708.   /* Allocate space for the text in the macro definition.
  5709.      Leading and trailing whitespace chars need 2 bytes each.
  5710.      Each other input char may or may not need 1 byte,
  5711.      so this is an upper bound.
  5712.      The extra 2 are for invented trailing newline-marker and final null.  */
  5713.   maxsize = (sizeof (DEFINITION)
  5714.          + 2 * (end - limit) + 2 * (p - buf)
  5715.          + (limit - p) + 3);
  5716.   defn = (DEFINITION *) xcalloc (1, maxsize);
  5717.  
  5718.   defn->nargs = nargs;
  5719.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  5720.   lastp = exp_p;
  5721.  
  5722.   p = buf;
  5723.  
  5724.   /* Convert leading whitespace to Newline-markers.  */
  5725.   while (p < limit && is_space[*p]) {
  5726.     *exp_p++ = '\n';
  5727.     *exp_p++ = *p++;
  5728.   }
  5729.  
  5730.   if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
  5731.     error ("`##' at start of macro definition");
  5732.     p += 2;
  5733.   }
  5734.  
  5735.   /* Process the main body of the definition.  */
  5736.   while (p < limit) {
  5737.     int skipped_arg = 0;
  5738.     register U_CHAR c = *p++;
  5739.  
  5740.     *exp_p++ = c;
  5741.  
  5742.     if (!traditional) {
  5743.       switch (c) {
  5744.       case '\'':
  5745.       case '\"':
  5746.         if (expected_delimiter != '\0') {
  5747.           if (c == expected_delimiter)
  5748.             expected_delimiter = '\0';
  5749.         } else
  5750.           expected_delimiter = c;
  5751.     break;
  5752.  
  5753.       case '\\':
  5754.     if (p < limit && expected_delimiter) {
  5755.       /* In a string, backslash goes through
  5756.          and makes next char ordinary.  */
  5757.       *exp_p++ = *p++;
  5758.     }
  5759.     break;
  5760.  
  5761.       case '#':
  5762.     /* # is ordinary inside a string.  */
  5763.     if (expected_delimiter)
  5764.       break;
  5765.     if (p < limit && *p == '#') {
  5766.       /* ##: concatenate preceding and following tokens.  */
  5767.       /* Take out the first #, discard preceding whitespace.  */
  5768.       exp_p--;
  5769.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  5770.         --exp_p;
  5771.       /* Skip the second #.  */
  5772.       p++;
  5773.       /* Discard following whitespace.  */
  5774.       SKIP_WHITE_SPACE (p);
  5775.       concat = p;
  5776.       if (p == limit)
  5777.         error ("`##' at end of macro definition");
  5778.     } else if (nargs >= 0) {
  5779.       /* Single #: stringify following argument ref.
  5780.          Don't leave the # in the expansion.  */
  5781.       exp_p--;
  5782.       SKIP_WHITE_SPACE (p);
  5783.       if (p == limit || ! is_idstart[*p])
  5784.         error ("`#' operator is not followed by a macro argument name");
  5785.       else
  5786.         stringify = p;
  5787.     }
  5788.     break;
  5789.       }
  5790.     } else {
  5791.       /* In -traditional mode, recognize arguments inside strings and
  5792.      and character constants, and ignore special properties of #.
  5793.      Arguments inside strings are considered "stringified", but no
  5794.      extra quote marks are supplied.  */
  5795.       switch (c) {
  5796.       case '\'':
  5797.       case '\"':
  5798.     if (expected_delimiter != '\0') {
  5799.       if (c == expected_delimiter)
  5800.         expected_delimiter = '\0';
  5801.     } else
  5802.       expected_delimiter = c;
  5803.     break;
  5804.  
  5805.       case '\\':
  5806.     /* Backslash quotes delimiters and itself, but not macro args.  */
  5807.     if (expected_delimiter != 0 && p < limit
  5808.         && (*p == expected_delimiter || *p == '\\')) {
  5809.       *exp_p++ = *p++;
  5810.       continue;
  5811.     }
  5812.     break;
  5813.  
  5814.       case '/':
  5815.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  5816.       break;
  5817.     if (*p == '*') {
  5818.       /* If we find a comment that wasn't removed by handle_directive,
  5819.          this must be -traditional.  So replace the comment with
  5820.          nothing at all.  */
  5821.       exp_p--;
  5822.       p += 1;
  5823.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  5824.         p++;
  5825. #if 0
  5826.       /* Mark this as a concatenation-point, as if it had been ##.  */
  5827.       concat = p;
  5828. #endif
  5829.     }
  5830.     break;
  5831.       }
  5832.     }
  5833.  
  5834.     /* Handle the start of a symbol.  */
  5835.     if (is_idchar[c] && nargs > 0) {
  5836.       U_CHAR *id_beg = p - 1;
  5837.       int id_len;
  5838.  
  5839.       --exp_p;
  5840.       while (p != limit && is_idchar[*p]) p++;
  5841.       id_len = p - id_beg;
  5842.  
  5843.       if (is_idstart[c]) {
  5844.     register struct arglist *arg;
  5845.  
  5846.     for (arg = arglist; arg != NULL; arg = arg->next) {
  5847.       struct reflist *tpat;
  5848.  
  5849.       if (arg->name[0] == c
  5850.           && arg->length == id_len
  5851.           && strncmp (arg->name, id_beg, id_len) == 0) {
  5852.         if (expected_delimiter && warn_stringify) {
  5853.           if (traditional) {
  5854.         warning ("macro argument `%.*s' is stringified.",
  5855.              id_len, arg->name);
  5856.           } else {
  5857.         warning ("macro arg `%.*s' would be stringified with -traditional.",
  5858.              id_len, arg->name);
  5859.           }
  5860.         }
  5861.         /* If ANSI, don't actually substitute inside a string.  */
  5862.         if (!traditional && expected_delimiter)
  5863.           break;
  5864.         /* make a pat node for this arg and append it to the end of
  5865.            the pat list */
  5866.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  5867.         tpat->next = NULL;
  5868.         tpat->raw_before = concat == id_beg;
  5869.         tpat->raw_after = 0;
  5870.         tpat->rest_args = arg->rest_args;
  5871.         tpat->stringify = (traditional ? expected_delimiter != '\0'
  5872.                    : stringify == id_beg);
  5873.  
  5874.         if (endpat == NULL)
  5875.           defn->pattern = tpat;
  5876.         else
  5877.           endpat->next = tpat;
  5878.         endpat = tpat;
  5879.  
  5880.         tpat->argno = arg->argno;
  5881.         tpat->nchars = exp_p - lastp;
  5882.         {
  5883.           register U_CHAR *p1 = p;
  5884.           SKIP_WHITE_SPACE (p1);
  5885.           if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  5886.         tpat->raw_after = 1;
  5887.         }
  5888.         lastp = exp_p;    /* place to start copying from next time */
  5889.         skipped_arg = 1;
  5890.         break;
  5891.       }
  5892.     }
  5893.       }
  5894.  
  5895.       /* If this was not a macro arg, copy it into the expansion.  */
  5896.       if (! skipped_arg) {
  5897.     register U_CHAR *lim1 = p;
  5898.     p = id_beg;
  5899.     while (p != lim1)
  5900.       *exp_p++ = *p++;
  5901.     if (stringify == id_beg)
  5902.       error ("`#' operator should be followed by a macro argument name");
  5903.       }
  5904.     }
  5905.   }
  5906.  
  5907.   if (!traditional && expected_delimiter == 0) {
  5908.     /* There is no trailing whitespace, so invent some in ANSI mode.
  5909.        But not if "inside a string" (which in ANSI mode
  5910.        happens only for -D option).  */
  5911.     *exp_p++ = '\n';
  5912.     *exp_p++ = ' ';
  5913.   }
  5914.  
  5915.   *exp_p = '\0';
  5916.  
  5917.   defn->length = exp_p - defn->expansion;
  5918.  
  5919.   /* Crash now if we overrun the allocated size.  */
  5920.   if (defn->length + 1 > maxsize)
  5921.     abort ();
  5922.  
  5923. #if 0
  5924. /* This isn't worth the time it takes.  */
  5925.   /* give back excess storage */
  5926.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  5927. #endif
  5928.  
  5929.   return defn;
  5930. }
  5931.  
  5932. static int
  5933. do_assert (buf, limit, op, keyword)
  5934.      U_CHAR *buf, *limit;
  5935.      FILE_BUF *op;
  5936.      struct directive *keyword;
  5937. {
  5938.   U_CHAR *bp;            /* temp ptr into input buffer */
  5939.   U_CHAR *symname;        /* remember where symbol name starts */
  5940.   int sym_length;        /* and how long it is */
  5941.   struct arglist *tokens = NULL;
  5942.  
  5943.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  5944.     pedwarn ("ANSI C does not allow `#assert'");
  5945.  
  5946.   bp = buf;
  5947.  
  5948.   while (is_hor_space[*bp])
  5949.     bp++;
  5950.  
  5951.   symname = bp;            /* remember where it starts */
  5952.   sym_length = check_macro_name (bp, "assertion");
  5953.   bp += sym_length;
  5954.   /* #define doesn't do this, but we should.  */
  5955.   SKIP_WHITE_SPACE (bp);
  5956.  
  5957.   /* Lossage will occur if identifiers or control tokens are broken
  5958.      across lines using backslash.  This is not the right place to take
  5959.      care of that. */
  5960.  
  5961.   if (*bp != '(') {
  5962.     error ("missing token-sequence in `#assert'");
  5963.     return 1;
  5964.   }
  5965.  
  5966.   {
  5967.     int error_flag = 0;
  5968.  
  5969.     bp++;            /* skip '(' */
  5970.     SKIP_WHITE_SPACE (bp);
  5971.  
  5972.     tokens = read_token_list (&bp, limit, &error_flag);
  5973.     if (error_flag)
  5974.       return 1;
  5975.     if (tokens == 0) {
  5976.       error ("empty token-sequence in `#assert'");
  5977.       return 1;
  5978.     }
  5979.  
  5980.     ++bp;            /* skip paren */
  5981.     SKIP_WHITE_SPACE (bp);
  5982.   }
  5983.  
  5984.   /* If this name isn't already an assertion name, make it one.
  5985.      Error if it was already in use in some other way.  */
  5986.  
  5987.   {
  5988.     ASSERTION_HASHNODE *hp;
  5989.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  5990.     struct tokenlist_list *value
  5991.       = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
  5992.  
  5993.     hp = assertion_lookup (symname, sym_length, hashcode);
  5994.     if (hp == NULL) {
  5995.       if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
  5996.     error ("`defined' redefined as assertion");
  5997.       hp = assertion_install (symname, sym_length, hashcode);
  5998.     }
  5999.  
  6000.     /* Add the spec'd token-sequence to the list of such.  */
  6001.     value->tokens = tokens;
  6002.     value->next = hp->value;
  6003.     hp->value = value;
  6004.   }
  6005.  
  6006.   return 0;
  6007. }
  6008.  
  6009. static int
  6010. do_unassert (buf, limit, op, keyword)
  6011.      U_CHAR *buf, *limit;
  6012.      FILE_BUF *op;
  6013.      struct directive *keyword;
  6014. {
  6015.   U_CHAR *bp;            /* temp ptr into input buffer */
  6016.   U_CHAR *symname;        /* remember where symbol name starts */
  6017.   int sym_length;        /* and how long it is */
  6018.  
  6019.   struct arglist *tokens = NULL;
  6020.   int tokens_specified = 0;
  6021.  
  6022.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  6023.     pedwarn ("ANSI C does not allow `#unassert'");
  6024.  
  6025.   bp = buf;
  6026.  
  6027.   while (is_hor_space[*bp])
  6028.     bp++;
  6029.  
  6030.   symname = bp;            /* remember where it starts */
  6031.   sym_length = check_macro_name (bp, "assertion");
  6032.   bp += sym_length;
  6033.   /* #define doesn't do this, but we should.  */
  6034.   SKIP_WHITE_SPACE (bp);
  6035.  
  6036.   /* Lossage will occur if identifiers or control tokens are broken
  6037.      across lines using backslash.  This is not the right place to take
  6038.      care of that. */
  6039.  
  6040.   if (*bp == '(') {
  6041.     int error_flag = 0;
  6042.  
  6043.     bp++;            /* skip '(' */
  6044.     SKIP_WHITE_SPACE (bp);
  6045.  
  6046.     tokens = read_token_list (&bp, limit, &error_flag);
  6047.     if (error_flag)
  6048.       return 1;
  6049.     if (tokens == 0) {
  6050.       error ("empty token list in `#unassert'");
  6051.       return 1;
  6052.     }
  6053.  
  6054.     tokens_specified = 1;
  6055.  
  6056.     ++bp;            /* skip paren */
  6057.     SKIP_WHITE_SPACE (bp);
  6058.   }
  6059.  
  6060.   {
  6061.     ASSERTION_HASHNODE *hp;
  6062.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  6063.     struct tokenlist_list *tail, *prev;
  6064.  
  6065.     hp = assertion_lookup (symname, sym_length, hashcode);
  6066.     if (hp == NULL)
  6067.       return 1;
  6068.  
  6069.     /* If no token list was specified, then eliminate this assertion
  6070.        entirely.  */
  6071.     if (! tokens_specified) {
  6072.       struct tokenlist_list *next;
  6073.       for (tail = hp->value; tail; tail = next) {
  6074.     next = tail->next;
  6075.     free_token_list (tail->tokens);
  6076.     free (tail);
  6077.       }
  6078.       delete_assertion (hp);
  6079.     } else {
  6080.       /* If a list of tokens was given, then delete any matching list.  */
  6081.  
  6082.       tail = hp->value;
  6083.       prev = 0;
  6084.       while (tail) {
  6085.     struct tokenlist_list *next = tail->next;
  6086.     if (compare_token_lists (tail->tokens, tokens)) {
  6087.       if (prev)
  6088.         prev->next = next;
  6089.       else
  6090.         hp->value = tail->next;
  6091.       free_token_list (tail->tokens);
  6092.       free (tail);
  6093.     } else {
  6094.       prev = tail;
  6095.     }
  6096.     tail = next;
  6097.       }
  6098.     }
  6099.   }
  6100.  
  6101.   return 0;
  6102. }
  6103.  
  6104. /* Test whether there is an assertion named NAME
  6105.    and optionally whether it has an asserted token list TOKENS.
  6106.    NAME is not null terminated; its length is SYM_LENGTH.
  6107.    If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
  6108.  
  6109. int
  6110. check_assertion (name, sym_length, tokens_specified, tokens)
  6111.      U_CHAR *name;
  6112.      int sym_length;
  6113.      int tokens_specified;
  6114.      struct arglist *tokens;
  6115. {
  6116.   ASSERTION_HASHNODE *hp;
  6117.   int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
  6118.  
  6119.   if (pedantic && !instack[indepth].system_header_p)
  6120.     pedwarn ("ANSI C does not allow testing assertions");
  6121.  
  6122.   hp = assertion_lookup (name, sym_length, hashcode);
  6123.   if (hp == NULL)
  6124.     /* It is not an assertion; just return false.  */
  6125.     return 0;
  6126.  
  6127.   /* If no token list was specified, then value is 1.  */
  6128.   if (! tokens_specified)
  6129.     return 1;
  6130.  
  6131.   {
  6132.     struct tokenlist_list *tail;
  6133.  
  6134.     tail = hp->value;
  6135.  
  6136.     /* If a list of tokens was given,
  6137.        then succeed if the assertion records a matching list.  */
  6138.  
  6139.     while (tail) {
  6140.       if (compare_token_lists (tail->tokens, tokens))
  6141.     return 1;
  6142.       tail = tail->next;
  6143.     }
  6144.  
  6145.     /* Fail if the assertion has no matching list.  */
  6146.     return 0;
  6147.   }
  6148. }
  6149.  
  6150. /* Compare two lists of tokens for equality including order of tokens.  */
  6151.  
  6152. static int
  6153. compare_token_lists (l1, l2)
  6154.      struct arglist *l1, *l2;
  6155. {
  6156.   while (l1 && l2) {
  6157.     if (l1->length != l2->length)
  6158.       return 0;
  6159.     if (strncmp (l1->name, l2->name, l1->length))
  6160.       return 0;
  6161.     l1 = l1->next;
  6162.     l2 = l2->next;
  6163.   }
  6164.  
  6165.   /* Succeed if both lists end at the same time.  */
  6166.   return l1 == l2;
  6167. }
  6168.  
  6169. /* Read a space-separated list of tokens ending in a close parenthesis.
  6170.    Return a list of strings, in the order they were written.
  6171.    (In case of error, return 0 and store -1 in *ERROR_FLAG.)
  6172.    Parse the text starting at *BPP, and update *BPP.
  6173.    Don't parse beyond LIMIT.  */
  6174.  
  6175. static struct arglist *
  6176. read_token_list (bpp, limit, error_flag)
  6177.      U_CHAR **bpp;
  6178.      U_CHAR *limit;
  6179.      int *error_flag;
  6180. {
  6181.   struct arglist *token_ptrs = 0;
  6182.   U_CHAR *bp = *bpp;
  6183.   int depth = 1;
  6184.  
  6185.   *error_flag = 0;
  6186.  
  6187.   /* Loop over the assertion value tokens.  */
  6188.   while (depth > 0) {
  6189.     struct arglist *temp;
  6190.     int eofp = 0;
  6191.     U_CHAR *beg = bp;
  6192.  
  6193.     /* Find the end of the token.  */
  6194.     if (*bp == '(') {
  6195.       bp++;
  6196.       depth++;
  6197.     } else if (*bp == ')') {
  6198.       depth--;
  6199.       if (depth == 0)
  6200.     break;
  6201.       bp++;
  6202.     } else if (*bp == '"' || *bp == '\'')
  6203.       bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  6204.     else
  6205.       while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
  6206.          && *bp != '"' && *bp != '\'' && bp != limit)
  6207.     bp++;
  6208.  
  6209.     temp = (struct arglist *) xmalloc (sizeof (struct arglist));
  6210.     temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
  6211.     bcopy ((char *) beg, (char *) temp->name, bp - beg);
  6212.     temp->name[bp - beg] = 0;
  6213.     temp->next = token_ptrs;
  6214.     token_ptrs = temp;
  6215.     temp->length = bp - beg;
  6216.  
  6217.     SKIP_WHITE_SPACE (bp);
  6218.  
  6219.     if (bp >= limit) {
  6220.       error ("unterminated token sequence in `#assert' or `#unassert'");
  6221.       *error_flag = -1;
  6222.       return 0;
  6223.     }
  6224.   }
  6225.   *bpp = bp;
  6226.  
  6227.   /* We accumulated the names in reverse order.
  6228.      Now reverse them to get the proper order.  */
  6229.   {
  6230.     register struct arglist *prev = 0, *this, *next;
  6231.     for (this = token_ptrs; this; this = next) {
  6232.       next = this->next;
  6233.       this->next = prev;
  6234.       prev = this;
  6235.     }
  6236.     return prev;
  6237.   }
  6238. }
  6239.  
  6240. static void
  6241. free_token_list (tokens)
  6242.      struct arglist *tokens;
  6243. {
  6244.   while (tokens) {
  6245.     struct arglist *next = tokens->next;
  6246.     free (tokens->name);
  6247.     free (tokens);
  6248.     tokens = next;
  6249.   }
  6250. }
  6251.  
  6252. /*
  6253.  * Install a name in the assertion hash table.
  6254.  *
  6255.  * If LEN is >= 0, it is the length of the name.
  6256.  * Otherwise, compute the length by scanning the entire name.
  6257.  *
  6258.  * If HASH is >= 0, it is the precomputed hash code.
  6259.  * Otherwise, compute the hash code.
  6260.  */
  6261. static ASSERTION_HASHNODE *
  6262. assertion_install (name, len, hash)
  6263.      U_CHAR *name;
  6264.      int len;
  6265.      int hash;
  6266. {
  6267.   register ASSERTION_HASHNODE *hp;
  6268.   register int i, bucket;
  6269.   register U_CHAR *p, *q;
  6270.  
  6271.   i = sizeof (ASSERTION_HASHNODE) + len + 1;
  6272.   hp = (ASSERTION_HASHNODE *) xmalloc (i);
  6273.   bucket = hash;
  6274.   hp->bucket_hdr = &assertion_hashtab[bucket];
  6275.   hp->next = assertion_hashtab[bucket];
  6276.   assertion_hashtab[bucket] = hp;
  6277.   hp->prev = NULL;
  6278.   if (hp->next != NULL)
  6279.     hp->next->prev = hp;
  6280.   hp->length = len;
  6281.   hp->value = 0;
  6282.   hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
  6283.   p = hp->name;
  6284.   q = name;
  6285.   for (i = 0; i < len; i++)
  6286.     *p++ = *q++;
  6287.   hp->name[len] = 0;
  6288.   return hp;
  6289. }
  6290.  
  6291. /*
  6292.  * find the most recent hash node for name name (ending with first
  6293.  * non-identifier char) installed by install
  6294.  *
  6295.  * If LEN is >= 0, it is the length of the name.
  6296.  * Otherwise, compute the length by scanning the entire name.
  6297.  *
  6298.  * If HASH is >= 0, it is the precomputed hash code.
  6299.  * Otherwise, compute the hash code.
  6300.  */
  6301. static ASSERTION_HASHNODE *
  6302. assertion_lookup (name, len, hash)
  6303.      U_CHAR *name;
  6304.      int len;
  6305.      int hash;
  6306. {
  6307.   register ASSERTION_HASHNODE *bucket;
  6308.  
  6309.   bucket = assertion_hashtab[hash];
  6310.   while (bucket) {
  6311.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  6312.       return bucket;
  6313.     bucket = bucket->next;
  6314.   }
  6315.   return NULL;
  6316. }
  6317.  
  6318. static void
  6319. delete_assertion (hp)
  6320.      ASSERTION_HASHNODE *hp;
  6321. {
  6322.  
  6323.   if (hp->prev != NULL)
  6324.     hp->prev->next = hp->next;
  6325.   if (hp->next != NULL)
  6326.     hp->next->prev = hp->prev;
  6327.  
  6328.   /* make sure that the bucket chain header that
  6329.      the deleted guy was on points to the right thing afterwards. */
  6330.   if (hp == *hp->bucket_hdr)
  6331.     *hp->bucket_hdr = hp->next;
  6332.  
  6333.   free (hp);
  6334. }
  6335.  
  6336. /*
  6337.  * interpret #line command.  Remembers previously seen fnames
  6338.  * in its very own hash table.
  6339.  */
  6340. #define FNAME_HASHSIZE 37
  6341.  
  6342. static int
  6343. do_line (buf, limit, op, keyword)
  6344.      U_CHAR *buf, *limit;
  6345.      FILE_BUF *op;
  6346.      struct directive *keyword;
  6347. {
  6348.   register U_CHAR *bp;
  6349.   FILE_BUF *ip = &instack[indepth];
  6350.   FILE_BUF tem;
  6351.   int new_lineno;
  6352.   enum file_change_code file_change = same_file;
  6353.  
  6354.   /* Expand any macros.  */
  6355.   tem = expand_to_temp_buffer (buf, limit, 0, 0);
  6356.  
  6357.   /* Point to macroexpanded line, which is null-terminated now.  */
  6358.   bp = tem.buf;
  6359.   SKIP_WHITE_SPACE (bp);
  6360.  
  6361.   if (!isdigit (*bp)) {
  6362.     error ("invalid format `#line' command");
  6363.     return 0;
  6364.   }
  6365.  
  6366.   /* The Newline at the end of this line remains to be processed.
  6367.      To put the next line at the specified line number,
  6368.      we must store a line number now that is one less.  */
  6369.   new_lineno = atoi (bp) - 1;
  6370.  
  6371.   /* NEW_LINENO is one less than the actual line number here.  */
  6372.   if (pedantic && new_lineno < 0)
  6373.     pedwarn ("line number out of range in `#line' command");
  6374.  
  6375.   /* skip over the line number.  */
  6376.   while (isdigit (*bp))
  6377.     bp++;
  6378.  
  6379. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  6380.   if (*bp && !is_space[*bp]) {
  6381.     error ("invalid format `#line' command");
  6382.     return;
  6383.   }
  6384. #endif
  6385.  
  6386.   SKIP_WHITE_SPACE (bp);
  6387.  
  6388.   if (*bp == '\"') {
  6389.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  6390.     HASHNODE *hp, **hash_bucket;
  6391.     U_CHAR *fname, *p;
  6392.     int fname_length;
  6393.  
  6394.     fname = ++bp;
  6395.  
  6396.     /* Turn the file name, which is a character string literal,
  6397.        into a null-terminated string.  Do this in place.  */
  6398.     p = bp;
  6399.     for (;;)
  6400.       switch ((*p++ = *bp++)) {
  6401.       case '\0':
  6402.     error ("invalid format `#line' command");
  6403.     return 0;
  6404.  
  6405.       case '\\':
  6406.     {
  6407.       char *bpc = (char *) bp;
  6408.       int c = parse_escape (&bpc);
  6409.       bp = (U_CHAR *) bpc;
  6410.       if (c < 0)
  6411.         p--;
  6412.       else
  6413.         p[-1] = c;
  6414.     }
  6415.     break;
  6416.  
  6417.       case '\"':
  6418.     p[-1] = 0;
  6419.     goto fname_done;
  6420.       }
  6421.   fname_done:
  6422.     fname_length = p - fname;
  6423.  
  6424.     SKIP_WHITE_SPACE (bp);
  6425.     if (*bp) {
  6426.       if (pedantic)
  6427.     pedwarn ("garbage at end of `#line' command");
  6428.       if (*bp == '1')
  6429.     file_change = enter_file;
  6430.       else if (*bp == '2')
  6431.     file_change = leave_file;
  6432.       else if (*bp == '3')
  6433.     ip->system_header_p = 1;
  6434.       else if (*bp == '4')
  6435.     ip->system_header_p = 2;
  6436.       else {
  6437.     error ("invalid format `#line' command");
  6438.     return 0;
  6439.       }
  6440.  
  6441.       bp++;
  6442.       SKIP_WHITE_SPACE (bp);
  6443.       if (*bp == '3') {
  6444.     ip->system_header_p = 1;
  6445.     bp++;
  6446.     SKIP_WHITE_SPACE (bp);
  6447.       }
  6448.       if (*bp == '4') {
  6449.     ip->system_header_p = 2;
  6450.     bp++;
  6451.     SKIP_WHITE_SPACE (bp);
  6452.       }
  6453.       if (*bp) {
  6454.     error ("invalid format `#line' command");
  6455.     return 0;
  6456.       }
  6457.     }
  6458.  
  6459.     hash_bucket =
  6460.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  6461.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  6462.       if (hp->length == fname_length &&
  6463.       strncmp (hp->value.cpval, fname, fname_length) == 0) {
  6464.     ip->nominal_fname = hp->value.cpval;
  6465.     break;
  6466.       }
  6467.     if (hp == 0) {
  6468.       /* Didn't find it; cons up a new one.  */
  6469.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  6470.       hp->next = *hash_bucket;
  6471.       *hash_bucket = hp;
  6472.  
  6473.       hp->length = fname_length;
  6474.       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  6475.       bcopy (fname, hp->value.cpval, fname_length);
  6476.     }
  6477.   } else if (*bp) {
  6478.     error ("invalid format `#line' command");
  6479.     return 0;
  6480.   }
  6481.  
  6482.   ip->lineno = new_lineno;
  6483.   output_line_command (ip, op, 0, file_change);
  6484.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  6485.   return 0;
  6486. }
  6487.  
  6488. /*
  6489.  * remove the definition of a symbol from the symbol table.
  6490.  * according to un*x /lib/cpp, it is not an error to undef
  6491.  * something that has no definitions, so it isn't one here either.
  6492.  */
  6493.  
  6494. static int
  6495. do_undef (buf, limit, op, keyword)
  6496.      U_CHAR *buf, *limit;
  6497.      FILE_BUF *op;
  6498.      struct directive *keyword;
  6499. {
  6500.   int sym_length;
  6501.   HASHNODE *hp;
  6502.   U_CHAR *orig_buf = buf;
  6503.  
  6504.   /* If this is a precompiler run (with -pcp) pass thru #undef commands.  */
  6505.   if (pcp_outfile && op)
  6506.     pass_thru_directive (buf, limit, op, keyword);
  6507.  
  6508.   SKIP_WHITE_SPACE (buf);
  6509.   sym_length = check_macro_name (buf, "macro");
  6510.  
  6511.   while ((hp = lookup (buf, sym_length, -1)) != NULL) {
  6512.     /* If we are generating additional info for debugging (with -g) we
  6513.        need to pass through all effective #undef commands.  */
  6514.     if (debug_output && op)
  6515.       pass_thru_directive (orig_buf, limit, op, keyword);
  6516.     if (hp->type != T_MACRO)
  6517.       warning ("undefining `%s'", hp->name);
  6518.     delete_macro (hp);
  6519.   }
  6520.  
  6521.   if (pedantic) {
  6522.     buf += sym_length;
  6523.     SKIP_WHITE_SPACE (buf);
  6524.     if (buf != limit)
  6525.       pedwarn ("garbage after `#undef' directive");
  6526.   }
  6527.   return 0;
  6528. }
  6529.  
  6530. /*
  6531.  * Report an error detected by the program we are processing.
  6532.  * Use the text of the line in the error message.
  6533.  * (We use error because it prints the filename & line#.)
  6534.  */
  6535.  
  6536. static int
  6537. do_error (buf, limit, op, keyword)
  6538.      U_CHAR *buf, *limit;
  6539.      FILE_BUF *op;
  6540.      struct directive *keyword;
  6541. {
  6542.   int length = limit - buf;
  6543.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  6544.   bcopy ((char *) buf, (char *) copy, length);
  6545.   copy[length] = 0;
  6546.   SKIP_WHITE_SPACE (copy);
  6547.   error ("#error %s", copy);
  6548.   return 0;
  6549. }
  6550.  
  6551. /*
  6552.  * Report a warning detected by the program we are processing.
  6553.  * Use the text of the line in the warning message, then continue.
  6554.  * (We use error because it prints the filename & line#.)
  6555.  */
  6556.  
  6557. static int
  6558. do_warning (buf, limit, op, keyword)
  6559.      U_CHAR *buf, *limit;
  6560.      FILE_BUF *op;
  6561.      struct directive *keyword;
  6562. {
  6563.   int length = limit - buf;
  6564.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  6565.   bcopy ((char *) buf, (char *) copy, length);
  6566.   copy[length] = 0;
  6567.   SKIP_WHITE_SPACE (copy);
  6568.   warning ("#warning %s", copy);
  6569.   return 0;
  6570. }
  6571.  
  6572. /* Remember the name of the current file being read from so that we can
  6573.    avoid ever including it again.  */
  6574.  
  6575. static int
  6576. do_once ()
  6577. {
  6578.   int i;
  6579.   FILE_BUF *ip = NULL;
  6580.  
  6581.   for (i = indepth; i >= 0; i--)
  6582.     if (instack[i].fname != NULL) {
  6583.       ip = &instack[i];
  6584.       break;
  6585.     }
  6586.  
  6587.   if (ip != NULL) {
  6588.     struct file_name_list *new;
  6589.     
  6590.     new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  6591.     new->next = dont_repeat_files;
  6592.     dont_repeat_files = new;
  6593.     new->fname = savestring (ip->fname);
  6594.     new->control_macro = 0;
  6595.     new->got_name_map = 0;
  6596.     new->c_system_include_path = 0;
  6597.   }
  6598.   return 0;
  6599. }
  6600.  
  6601. /* #ident has already been copied to the output file, so just ignore it.  */
  6602.  
  6603. static int
  6604. do_ident (buf, limit)
  6605.      U_CHAR *buf, *limit;
  6606. {
  6607.   FILE_BUF trybuf;
  6608.   int len;
  6609.   FILE_BUF *op = &outbuf;
  6610.  
  6611.   /* Allow #ident in system headers, since that's not user's fault.  */
  6612.   if (pedantic && !instack[indepth].system_header_p)
  6613.     pedwarn ("ANSI C does not allow `#ident'");
  6614.  
  6615.   trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  6616.   buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  6617.   bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  6618.   limit = buf + (trybuf.bufp - trybuf.buf);
  6619.   len = (limit - buf);
  6620.   free (trybuf.buf);
  6621.  
  6622.   /* Output directive name.  */
  6623.   check_expand (op, 7);
  6624.   bcopy ("#ident ", (char *) op->bufp, 7);
  6625.   op->bufp += 7;
  6626.  
  6627.   /* Output the expanded argument line.  */
  6628.   check_expand (op, len);
  6629.   bcopy ((char *) buf, (char *) op->bufp, len);
  6630.   op->bufp += len;
  6631.  
  6632.   return 0;
  6633. }
  6634.  
  6635. /* #pragma and its argument line have already been copied to the output file.
  6636.    Just check for some recognized pragmas that need validation here.  */
  6637.  
  6638. static int
  6639. do_pragma (buf, limit)
  6640.      U_CHAR *buf, *limit;
  6641. {
  6642.   while (*buf == ' ' || *buf == '\t')
  6643.     buf++;
  6644.   if (!strncmp (buf, "once", 4)) {
  6645.     /* Allow #pragma once in system headers, since that's not the user's
  6646.        fault.  */
  6647.     if (!instack[indepth].system_header_p)
  6648.       warning ("`#pragma once' is obsolete");
  6649.     do_once ();
  6650.   }
  6651.  
  6652.   if (!strncmp (buf, "implementation", 14)) {
  6653.     /* Be quiet about `#pragma implementation' for a file only if it hasn't
  6654.        been included yet.  */
  6655.     struct file_name_list *ptr;
  6656.     U_CHAR *p = buf + 14, *fname, *inc_fname;
  6657.     SKIP_WHITE_SPACE (p);
  6658.     if (*p == '\n' || *p != '\"')
  6659.       return 0;
  6660.  
  6661.     fname = p + 1;
  6662.     if (p = (U_CHAR *) index (fname, '\"'))
  6663.       *p = '\0';
  6664.     
  6665.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  6666.       inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
  6667.       inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
  6668.       if (inc_fname && !strcmp (inc_fname, fname))
  6669.     warning ("`#pragma implementation' for `%s' appears after file is included",
  6670.          fname);
  6671.     }
  6672.   }
  6673.  
  6674.   return 0;
  6675. }
  6676.  
  6677. #if 0
  6678. /* This was a fun hack, but #pragma seems to start to be useful.
  6679.    By failing to recognize it, we pass it through unchanged to cc1.  */
  6680.  
  6681. /*
  6682.  * the behavior of the #pragma directive is implementation defined.
  6683.  * this implementation defines it as follows.
  6684.  */
  6685.  
  6686. static int
  6687. do_pragma ()
  6688. {
  6689.   close (0);
  6690.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  6691.     goto nope;
  6692.   close (1);
  6693.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  6694.     goto nope;
  6695.   execl ("/usr/games/hack", "#pragma", 0);
  6696.   execl ("/usr/games/rogue", "#pragma", 0);
  6697.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  6698.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  6699. nope:
  6700.   fatal ("You are in a maze of twisty compiler features, all different");
  6701. }
  6702. #endif
  6703.  
  6704. /* Just ignore #sccs, on systems where we define it at all.  */
  6705.  
  6706. static int
  6707. do_sccs ()
  6708. {
  6709.   if (pedantic)
  6710.     pedwarn ("ANSI C does not allow `#sccs'");
  6711.   return 0;
  6712. }
  6713.  
  6714. /*
  6715.  * handle #if command by
  6716.  *   1) inserting special `defined' keyword into the hash table
  6717.  *    that gets turned into 0 or 1 by special_symbol (thus,
  6718.  *    if the luser has a symbol called `defined' already, it won't
  6719.  *      work inside the #if command)
  6720.  *   2) rescan the input into a temporary output buffer
  6721.  *   3) pass the output buffer to the yacc parser and collect a value
  6722.  *   4) clean up the mess left from steps 1 and 2.
  6723.  *   5) call conditional_skip to skip til the next #endif (etc.),
  6724.  *      or not, depending on the value from step 3.
  6725.  */
  6726.  
  6727. static int
  6728. do_if (buf, limit, op, keyword)
  6729.      U_CHAR *buf, *limit;
  6730.      FILE_BUF *op;
  6731.      struct directive *keyword;
  6732. {
  6733.   HOST_WIDE_INT value;
  6734.   FILE_BUF *ip = &instack[indepth];
  6735.  
  6736.   value = eval_if_expression (buf, limit - buf);
  6737.   conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
  6738.   return 0;
  6739. }
  6740.  
  6741. /*
  6742.  * handle a #elif directive by not changing  if_stack  either.
  6743.  * see the comment above do_else.
  6744.  */
  6745.  
  6746. static int
  6747. do_elif (buf, limit, op, keyword)
  6748.      U_CHAR *buf, *limit;
  6749.      FILE_BUF *op;
  6750.      struct directive *keyword;
  6751. {
  6752.   HOST_WIDE_INT value;
  6753.   FILE_BUF *ip = &instack[indepth];
  6754.  
  6755.   if (if_stack == instack[indepth].if_stack) {
  6756.     error ("`#elif' not within a conditional");
  6757.     return 0;
  6758.   } else {
  6759.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  6760.       error ("`#elif' after `#else'");
  6761.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  6762.       if (if_stack->fname != NULL && ip->fname != NULL &&
  6763.       strcmp (if_stack->fname, ip->nominal_fname) != 0)
  6764.     fprintf (stderr, ", file %s", if_stack->fname);
  6765.       fprintf (stderr, ")\n");
  6766.     }
  6767.     if_stack->type = T_ELIF;
  6768.   }
  6769.  
  6770.   if (if_stack->if_succeeded)
  6771.     skip_if_group (ip, 0, op);
  6772.   else {
  6773.     value = eval_if_expression (buf, limit - buf);
  6774.     if (value == 0)
  6775.       skip_if_group (ip, 0, op);
  6776.     else {
  6777.       ++if_stack->if_succeeded;    /* continue processing input */
  6778.       output_line_command (ip, op, 1, same_file);
  6779.     }
  6780.   }
  6781.   return 0;
  6782. }
  6783.  
  6784. /*
  6785.  * evaluate a #if expression in BUF, of length LENGTH,
  6786.  * then parse the result as a C expression and return the value as an int.
  6787.  */
  6788. static HOST_WIDE_INT
  6789. eval_if_expression (buf, length)
  6790.      U_CHAR *buf;
  6791.      int length;
  6792. {
  6793.   FILE_BUF temp_obuf;
  6794.   HASHNODE *save_defined;
  6795.   HOST_WIDE_INT value;
  6796.  
  6797.   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, NULL_PTR, -1);
  6798.   pcp_inside_if = 1;
  6799.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
  6800.   pcp_inside_if = 0;
  6801.   delete_macro (save_defined);    /* clean up special symbol */
  6802.  
  6803.   value = parse_c_expression (temp_obuf.buf);
  6804.  
  6805.   free (temp_obuf.buf);
  6806.  
  6807.   return value;
  6808. }
  6809.  
  6810. /*
  6811.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  6812.  * then do or don't skip to the #endif/#else/#elif depending
  6813.  * on what directive is actually being processed.
  6814.  */
  6815.  
  6816. static int
  6817. do_xifdef (buf, limit, op, keyword)
  6818.      U_CHAR *buf, *limit;
  6819.      FILE_BUF *op;
  6820.      struct directive *keyword;
  6821. {
  6822.   int skip;
  6823.   FILE_BUF *ip = &instack[indepth];
  6824.   U_CHAR *end; 
  6825.   int start_of_file = 0;
  6826.   U_CHAR *control_macro = 0;
  6827.  
  6828.   /* Detect a #ifndef at start of file (not counting comments).  */
  6829.   if (ip->fname != 0 && keyword->type == T_IFNDEF) {
  6830.     U_CHAR *p = ip->buf;
  6831.     while (p != directive_start) {
  6832.       U_CHAR c = *p++;
  6833.       if (is_space[c])
  6834.     ;
  6835.       else if (c == '/' && p != ip->bufp && *p == '*') {
  6836.     /* Skip this comment.  */
  6837.     int junk = 0;
  6838.     U_CHAR *save_bufp = ip->bufp;
  6839.     ip->bufp = p + 1;
  6840.     p = skip_to_end_of_comment (ip, &junk, 1);
  6841.     ip->bufp = save_bufp;
  6842.       } else {
  6843.     goto fail;
  6844.       }
  6845.     }
  6846.     /* If we get here, this conditional is the beginning of the file.  */
  6847.     start_of_file = 1;
  6848.   fail: ;
  6849.   }
  6850.  
  6851.   /* Discard leading and trailing whitespace.  */
  6852.   SKIP_WHITE_SPACE (buf);
  6853.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  6854.  
  6855.   /* Find the end of the identifier at the beginning.  */
  6856.   for (end = buf; is_idchar[*end]; end++);
  6857.  
  6858.   if (end == buf) {
  6859.     skip = (keyword->type == T_IFDEF);
  6860.     if (! traditional)
  6861.       pedwarn (end == limit ? "`#%s' with no argument"
  6862.            : "`#%s' argument starts with punctuation",
  6863.            keyword->name);
  6864.   } else {
  6865.     HASHNODE *hp;
  6866.  
  6867.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  6868.       pedwarn ("`#%s' argument starts with a digit", keyword->name);
  6869.     else if (end != limit && !traditional)
  6870.       pedwarn ("garbage at end of `#%s' argument", keyword->name);
  6871.  
  6872.     hp = lookup (buf, end-buf, -1);
  6873.  
  6874.     if (pcp_outfile) {
  6875.       /* Output a precondition for this macro.  */
  6876.       if (hp &&
  6877.       (hp->type == T_CONST
  6878.        || (hp->type == T_MACRO && hp->value.defn->predefined)))
  6879.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  6880.       else {
  6881.     U_CHAR *cp = buf;
  6882.     fprintf (pcp_outfile, "#undef ");
  6883.     while (is_idchar[*cp]) /* Ick! */
  6884.       fputc (*cp++, pcp_outfile);
  6885.     putc ('\n', pcp_outfile);
  6886.       }
  6887.     }
  6888.  
  6889.     skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
  6890.     if (start_of_file && !skip) {
  6891.       control_macro = (U_CHAR *) xmalloc (end - buf + 1);
  6892.       bcopy ((char *) buf, (char *) control_macro, end - buf);
  6893.       control_macro[end - buf] = 0;
  6894.     }
  6895.   }
  6896.   
  6897.   conditional_skip (ip, skip, T_IF, control_macro, op);
  6898.   return 0;
  6899. }
  6900.  
  6901. /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  6902.    If this is a #ifndef starting at the beginning of a file,
  6903.    CONTROL_MACRO is the macro name tested by the #ifndef.
  6904.    Otherwise, CONTROL_MACRO is 0.  */
  6905.  
  6906. static void
  6907. conditional_skip (ip, skip, type, control_macro, op)
  6908.      FILE_BUF *ip;
  6909.      int skip;
  6910.      enum node_type type;
  6911.      U_CHAR *control_macro;
  6912.      FILE_BUF *op;
  6913. {
  6914.   IF_STACK_FRAME *temp;
  6915.  
  6916.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  6917.   temp->fname = ip->nominal_fname;
  6918.   temp->lineno = ip->lineno;
  6919.   temp->next = if_stack;
  6920.   temp->control_macro = control_macro;
  6921.   if_stack = temp;
  6922.  
  6923.   if_stack->type = type;
  6924.  
  6925.   if (skip != 0) {
  6926.     skip_if_group (ip, 0, op);
  6927.     return;
  6928.   } else {
  6929.     ++if_stack->if_succeeded;
  6930.     output_line_command (ip, &outbuf, 1, same_file);
  6931.   }
  6932. }
  6933.  
  6934. /*
  6935.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  6936.  * leaves input ptr at the sharp sign found.
  6937.  * If ANY is nonzero, return at next directive of any sort.
  6938.  */
  6939. static void
  6940. skip_if_group (ip, any, op)
  6941.      FILE_BUF *ip;
  6942.      int any;
  6943.      FILE_BUF *op;
  6944. {
  6945.   register U_CHAR *bp = ip->bufp, *cp;
  6946.   register U_CHAR *endb = ip->buf + ip->length;
  6947.   struct directive *kt;
  6948.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  6949.   U_CHAR *beg_of_line = bp;
  6950.   register int ident_length;
  6951.   U_CHAR *ident, *after_ident;
  6952.   /* Save info about where the group starts.  */
  6953.   U_CHAR *beg_of_group = bp;
  6954.   int beg_lineno = ip->lineno;
  6955.  
  6956.   if (output_conditionals && op != 0) {
  6957.     char *ptr = "#failed\n";
  6958.     int len = strlen (ptr);
  6959.  
  6960.     if (op->bufp > op->buf && op->bufp[-1] != '\n')
  6961.       {
  6962.     *op->bufp++ = '\n';
  6963.     op->lineno++;
  6964.       }
  6965.     check_expand (op, len);
  6966.     bcopy (ptr, (char *) op->bufp, len);
  6967.     op->bufp += len;
  6968.     op->lineno++;
  6969.     output_line_command (ip, op, 1, 0);
  6970.   }
  6971.  
  6972.   while (bp < endb) {
  6973.     switch (*bp++) {
  6974.     case '/':            /* possible comment */
  6975.       if (*bp == '\\' && bp[1] == '\n')
  6976.     newline_fix (bp);
  6977.       if (*bp == '*'
  6978.       || (cplusplus_comments && *bp == '/')) {
  6979.     ip->bufp = ++bp;
  6980.     bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
  6981.       }
  6982.       break;
  6983.     case '\"':
  6984.     case '\'':
  6985.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
  6986.                    NULL_PTR, NULL_PTR);
  6987.       break;
  6988.     case '\\':
  6989.       /* Char after backslash loses its special meaning.  */
  6990.       if (bp < endb) {
  6991.     if (*bp == '\n')
  6992.       ++ip->lineno;        /* But do update the line-count.  */
  6993.     bp++;
  6994.       }
  6995.       break;
  6996.     case '\n':
  6997.       ++ip->lineno;
  6998.       beg_of_line = bp;
  6999.       break;
  7000.     case '#':
  7001.       ip->bufp = bp - 1;
  7002.  
  7003.       /* # keyword: a # must be first nonblank char on the line */
  7004.       if (beg_of_line == 0)
  7005.     break;
  7006.       /* Scan from start of line, skipping whitespace, comments
  7007.      and backslash-newlines, and see if we reach this #.
  7008.      If not, this # is not special.  */
  7009.       bp = beg_of_line;
  7010.       /* If -traditional, require # to be at beginning of line.  */
  7011.       if (!traditional)
  7012.     while (1) {
  7013.       if (is_hor_space[*bp])
  7014.         bp++;
  7015.       else if (*bp == '\\' && bp[1] == '\n')
  7016.         bp += 2;
  7017.       else if (*bp == '/' && bp[1] == '*') {
  7018.         bp += 2;
  7019.         while (!(*bp == '*' && bp[1] == '/'))
  7020.           bp++;
  7021.         bp += 2;
  7022.       }
  7023.       /* There is no point in trying to deal with C++ // comments here,
  7024.          because if there is one, then this # must be part of the
  7025.          comment and we would never reach here.  */
  7026.       else break;
  7027.     }
  7028.       if (bp != ip->bufp) {
  7029.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  7030.     break;
  7031.       }
  7032.  
  7033.       bp = ip->bufp + 1;    /* Point after the '#' */
  7034.  
  7035.       /* Skip whitespace and \-newline.  */
  7036.       while (1) {
  7037.     if (is_hor_space[*bp])
  7038.       bp++;
  7039.     else if (*bp == '\\' && bp[1] == '\n')
  7040.       bp += 2;
  7041.     else if (*bp == '/' && bp[1] == '*') {
  7042.       bp += 2;
  7043.       while (!(*bp == '*' && bp[1] == '/')) {
  7044.         if (*bp == '\n')
  7045.           ip->lineno++;
  7046.         bp++;
  7047.       }
  7048.       bp += 2;
  7049.     } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  7050.       bp += 2;
  7051.       while (bp[-1] == '\\' || *bp != '\n') {
  7052.         if (*bp == '\n')
  7053.           ip->lineno++;
  7054.         bp++;
  7055.       }
  7056.         }
  7057.     else break;
  7058.       }
  7059.  
  7060.       cp = bp;
  7061.  
  7062.       /* Now find end of directive name.
  7063.      If we encounter a backslash-newline, exchange it with any following
  7064.      symbol-constituents so that we end up with a contiguous name.  */
  7065.  
  7066.       while (1) {
  7067.     if (is_idchar[*bp])
  7068.       bp++;
  7069.     else {
  7070.       if (*bp == '\\' && bp[1] == '\n')
  7071.         name_newline_fix (bp);
  7072.       if (is_idchar[*bp])
  7073.         bp++;
  7074.       else break;
  7075.     }
  7076.       }
  7077.       ident_length = bp - cp;
  7078.       ident = cp;
  7079.       after_ident = bp;
  7080.  
  7081.       /* A line of just `#' becomes blank.  */
  7082.  
  7083.       if (ident_length == 0 && *after_ident == '\n') {
  7084.     continue;
  7085.       }
  7086.  
  7087.       if (ident_length == 0 || !is_idstart[*ident]) {
  7088.     U_CHAR *p = ident;
  7089.     while (is_idchar[*p]) {
  7090.       if (*p < '0' || *p > '9')
  7091.         break;
  7092.       p++;
  7093.     }
  7094.     /* Handle # followed by a line number.  */
  7095.     if (p != ident && !is_idchar[*p]) {
  7096.       if (pedantic)
  7097.         pedwarn ("`#' followed by integer");
  7098.       continue;
  7099.     }
  7100.  
  7101.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  7102.     if (p == ident) {
  7103.       while (*p == '#' || is_hor_space[*p]) p++;
  7104.       if (*p == '\n') {
  7105.         if (pedantic && !lang_asm)
  7106.           pedwarn ("invalid preprocessor directive");
  7107.         continue;
  7108.       }
  7109.     }
  7110.  
  7111.     if (!lang_asm && pedantic)
  7112.       pedwarn ("invalid preprocessor directive name");
  7113.     continue;
  7114.       }
  7115.  
  7116.       for (kt = directive_table; kt->length >= 0; kt++) {
  7117.     IF_STACK_FRAME *temp;
  7118.     if (ident_length == kt->length
  7119.         && strncmp (cp, kt->name, kt->length) == 0) {
  7120.       /* If we are asked to return on next directive, do so now.  */
  7121.       if (any)
  7122.         goto done;
  7123.  
  7124.       switch (kt->type) {
  7125.       case T_IF:
  7126.       case T_IFDEF:
  7127.       case T_IFNDEF:
  7128.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  7129.         temp->next = if_stack;
  7130.         if_stack = temp;
  7131.         temp->lineno = ip->lineno;
  7132.         temp->fname = ip->nominal_fname;
  7133.         temp->type = kt->type;
  7134.         break;
  7135.       case T_ELSE:
  7136.       case T_ENDIF:
  7137.         if (pedantic && if_stack != save_if_stack)
  7138.           validate_else (bp);
  7139.       case T_ELIF:
  7140.         if (if_stack == instack[indepth].if_stack) {
  7141.           error ("`#%s' not within a conditional", kt->name);
  7142.           break;
  7143.         }
  7144.         else if (if_stack == save_if_stack)
  7145.           goto done;        /* found what we came for */
  7146.  
  7147.         if (kt->type != T_ENDIF) {
  7148.           if (if_stack->type == T_ELSE)
  7149.         error ("`#else' or `#elif' after `#else'");
  7150.           if_stack->type = kt->type;
  7151.           break;
  7152.         }
  7153.  
  7154.         temp = if_stack;
  7155.         if_stack = if_stack->next;
  7156.         free (temp);
  7157.         break;
  7158.       }
  7159.       break;
  7160.     }
  7161.       }
  7162.       /* Don't let erroneous code go by.  */
  7163.       if (kt->length < 0 && !lang_asm && pedantic)
  7164.     pedwarn ("invalid preprocessor directive name");
  7165.     }
  7166.   }
  7167.  
  7168.   ip->bufp = bp;
  7169.   /* after this returns, rescan will exit because ip->bufp
  7170.      now points to the end of the buffer.
  7171.      rescan is responsible for the error message also.  */
  7172.  
  7173.  done:
  7174.   if (output_conditionals && op != 0) {
  7175.     char *ptr = "#endfailed\n";
  7176.     int len = strlen (ptr);
  7177.  
  7178.     if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7179.       {
  7180.     *op->bufp++ = '\n';
  7181.     op->lineno++;
  7182.       }
  7183.     check_expand (op, beg_of_line - beg_of_group);
  7184.     bcopy ((char *) beg_of_group, (char *) op->bufp,
  7185.        beg_of_line - beg_of_group);
  7186.     op->bufp += beg_of_line - beg_of_group;
  7187.     op->lineno += ip->lineno - beg_lineno;
  7188.     check_expand (op, len);
  7189.     bcopy (ptr, (char *) op->bufp, len);
  7190.     op->bufp += len;
  7191.     op->lineno++;
  7192.   }
  7193. }
  7194.  
  7195. /*
  7196.  * handle a #else directive.  Do this by just continuing processing
  7197.  * without changing  if_stack ;  this is so that the error message
  7198.  * for missing #endif's etc. will point to the original #if.  It
  7199.  * is possible that something different would be better.
  7200.  */
  7201.  
  7202. static int
  7203. do_else (buf, limit, op, keyword)
  7204.      U_CHAR *buf, *limit;
  7205.      FILE_BUF *op;
  7206.      struct directive *keyword;
  7207. {
  7208.   FILE_BUF *ip = &instack[indepth];
  7209.  
  7210.   if (pedantic) {
  7211.     SKIP_WHITE_SPACE (buf);
  7212.     if (buf != limit)
  7213.       pedwarn ("text following `#else' violates ANSI standard");
  7214.   }
  7215.  
  7216.   if (if_stack == instack[indepth].if_stack) {
  7217.     error ("`#else' not within a conditional");
  7218.     return 0;
  7219.   } else {
  7220.     /* #ifndef can't have its special treatment for containing the whole file
  7221.        if it has a #else clause.  */
  7222.     if_stack->control_macro = 0;
  7223.  
  7224.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  7225.       error ("`#else' after `#else'");
  7226.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  7227.       if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
  7228.     fprintf (stderr, ", file %s", if_stack->fname);
  7229.       fprintf (stderr, ")\n");
  7230.     }
  7231.     if_stack->type = T_ELSE;
  7232.   }
  7233.  
  7234.   if (if_stack->if_succeeded)
  7235.     skip_if_group (ip, 0, op);
  7236.   else {
  7237.     ++if_stack->if_succeeded;    /* continue processing input */
  7238.     output_line_command (ip, op, 1, same_file);
  7239.   }
  7240.   return 0;
  7241. }
  7242.  
  7243. /*
  7244.  * unstack after #endif command
  7245.  */
  7246.  
  7247. static int
  7248. do_endif (buf, limit, op, keyword)
  7249.      U_CHAR *buf, *limit;
  7250.      FILE_BUF *op;
  7251.      struct directive *keyword;
  7252. {
  7253.   if (pedantic) {
  7254.     SKIP_WHITE_SPACE (buf);
  7255.     if (buf != limit)
  7256.       pedwarn ("text following `#endif' violates ANSI standard");
  7257.   }
  7258.  
  7259.   if (if_stack == instack[indepth].if_stack)
  7260.     error ("unbalanced `#endif'");
  7261.   else {
  7262.     IF_STACK_FRAME *temp = if_stack;
  7263.     if_stack = if_stack->next;
  7264.     if (temp->control_macro != 0) {
  7265.       /* This #endif matched a #ifndef at the start of the file.
  7266.      See if it is at the end of the file.  */
  7267.       FILE_BUF *ip = &instack[indepth];
  7268.       U_CHAR *p = ip->bufp;
  7269.       U_CHAR *ep = ip->buf + ip->length;
  7270.  
  7271.       while (p != ep) {
  7272.     U_CHAR c = *p++;
  7273.     switch (c) {
  7274.     case ' ':
  7275.     case '\t':
  7276.     case '\n':
  7277.       break;
  7278.     case '/':
  7279.       if (p != ep && *p == '*') {
  7280.         /* Skip this comment.  */
  7281.         int junk = 0;
  7282.         U_CHAR *save_bufp = ip->bufp;
  7283.         ip->bufp = p + 1;
  7284.         p = skip_to_end_of_comment (ip, &junk, 1);
  7285.         ip->bufp = save_bufp;
  7286.       }
  7287.       break;
  7288.     default:
  7289.       goto fail;
  7290.     }
  7291.       }
  7292.       /* If we get here, this #endif ends a #ifndef
  7293.      that contains all of the file (aside from whitespace).
  7294.      Arrange not to include the file again
  7295.      if the macro that was tested is defined.
  7296.  
  7297.      Do not do this for the top-level file in a -include or any
  7298.      file in a -imacros.  */
  7299.       if (indepth != 0
  7300.       && ! (indepth == 1 && no_record_file)
  7301.       && ! (no_record_file && no_output))
  7302.     record_control_macro (ip->fname, temp->control_macro);
  7303.     fail: ;
  7304.     }
  7305.     free (temp);
  7306.     output_line_command (&instack[indepth], op, 1, same_file);
  7307.   }
  7308.   return 0;
  7309. }
  7310.  
  7311. /* When an #else or #endif is found while skipping failed conditional,
  7312.    if -pedantic was specified, this is called to warn about text after
  7313.    the command name.  P points to the first char after the command name.  */
  7314.  
  7315. static void
  7316. validate_else (p)
  7317.      register U_CHAR *p;
  7318. {
  7319.   /* Advance P over whitespace and comments.  */
  7320.   while (1) {
  7321.     if (*p == '\\' && p[1] == '\n')
  7322.       p += 2;
  7323.     if (is_hor_space[*p])
  7324.       p++;
  7325.     else if (*p == '/') {
  7326.       if (p[1] == '\\' && p[2] == '\n')
  7327.     newline_fix (p + 1);
  7328.       if (p[1] == '*') {
  7329.     p += 2;
  7330.     /* Don't bother warning about unterminated comments
  7331.        since that will happen later.  Just be sure to exit.  */
  7332.     while (*p) {
  7333.       if (p[1] == '\\' && p[2] == '\n')
  7334.         newline_fix (p + 1);
  7335.       if (*p == '*' && p[1] == '/') {
  7336.         p += 2;
  7337.         break;
  7338.       }
  7339.       p++;
  7340.     }
  7341.       }
  7342.       else if (cplusplus_comments && p[1] == '/') {
  7343.     p += 2;
  7344.     while (*p && (*p != '\n' || p[-1] == '\\'))
  7345.       p++;
  7346.       }
  7347.     } else break;
  7348.   }
  7349.   if (*p && *p != '\n')
  7350.     pedwarn ("text following `#else' or `#endif' violates ANSI standard");
  7351. }
  7352.  
  7353. /* Skip a comment, assuming the input ptr immediately follows the
  7354.    initial slash-star.  Bump *LINE_COUNTER for each newline.
  7355.    (The canonical line counter is &ip->lineno.)
  7356.    Don't use this routine (or the next one) if bumping the line
  7357.    counter is not sufficient to deal with newlines in the string.
  7358.  
  7359.    If NOWARN is nonzero, don't warn about slash-star inside a comment.
  7360.    This feature is useful when processing a comment that is going to be
  7361.    processed or was processed at another point in the preprocessor,
  7362.    to avoid a duplicate warning.  Likewise for unterminated comment errors.  */
  7363.  
  7364. static U_CHAR *
  7365. skip_to_end_of_comment (ip, line_counter, nowarn)
  7366.      register FILE_BUF *ip;
  7367.      int *line_counter;        /* place to remember newlines, or NULL */
  7368.      int nowarn;
  7369. {
  7370.   register U_CHAR *limit = ip->buf + ip->length;
  7371.   register U_CHAR *bp = ip->bufp;
  7372.   FILE_BUF *op = &outbuf;    /* JF */
  7373.   int output = put_out_comments && !line_counter;
  7374.   int start_line = line_counter ? *line_counter : 0;
  7375.  
  7376.     /* JF this line_counter stuff is a crock to make sure the
  7377.        comment is only put out once, no matter how many times
  7378.        the comment is skipped.  It almost works */
  7379.   if (output) {
  7380.     *op->bufp++ = '/';
  7381.     *op->bufp++ = '*';
  7382.   }
  7383.   if (cplusplus_comments && bp[-1] == '/') {
  7384.     if (output) {
  7385.       while (bp < limit) {
  7386.     *op->bufp++ = *bp;
  7387.     if (*bp == '\n' && bp[-1] != '\\')
  7388.       break;
  7389.     if (*bp == '\n') {
  7390.       ++*line_counter;
  7391.       ++op->lineno;
  7392.     }
  7393.     bp++;
  7394.       }
  7395.       op->bufp[-1] = '*';
  7396.       *op->bufp++ = '/';
  7397.       *op->bufp++ = '\n';
  7398.     } else {
  7399.       while (bp < limit) {
  7400.     if (bp[-1] != '\\' && *bp == '\n') {
  7401.       break;
  7402.     } else {
  7403.       if (*bp == '\n' && line_counter)
  7404.         ++*line_counter;
  7405.       bp++;
  7406.     }
  7407.       }
  7408.     }
  7409.     ip->bufp = bp;
  7410.     return bp;
  7411.   }
  7412.   while (bp < limit) {
  7413.     if (output)
  7414.       *op->bufp++ = *bp;
  7415.     switch (*bp++) {
  7416.     case '/':
  7417.       if (warn_comments && !nowarn && bp < limit && *bp == '*')
  7418.     warning ("`/*' within comment");
  7419.       break;
  7420.     case '\n':
  7421.       /* If this is the end of the file, we have an unterminated comment.
  7422.      Don't swallow the newline.  We are guaranteed that there will be a
  7423.      trailing newline and various pieces assume it's there.  */
  7424.       if (bp == limit)
  7425.     {
  7426.       --bp;
  7427.       --limit;
  7428.       break;
  7429.     }
  7430.       if (line_counter != NULL)
  7431.     ++*line_counter;
  7432.       if (output)
  7433.     ++op->lineno;
  7434.       break;
  7435.     case '*':
  7436.       if (*bp == '\\' && bp[1] == '\n')
  7437.     newline_fix (bp);
  7438.       if (*bp == '/') {
  7439.         if (output)
  7440.       *op->bufp++ = '/';
  7441.     ip->bufp = ++bp;
  7442.     return bp;
  7443.       }
  7444.       break;
  7445.     }
  7446.   }
  7447.  
  7448.   if (!nowarn)
  7449.     error_with_line (line_for_error (start_line), "unterminated comment");
  7450.   ip->bufp = bp;
  7451.   return bp;
  7452. }
  7453.  
  7454. /*
  7455.  * Skip over a quoted string.  BP points to the opening quote.
  7456.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  7457.  * START_LINE is the line number of the starting point (but it need
  7458.  * not be valid if the starting point is inside a macro expansion).
  7459.  *
  7460.  * The input stack state is not changed.
  7461.  *
  7462.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  7463.  * for each newline passed.
  7464.  *
  7465.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  7466.  * if we pass a backslash-newline.
  7467.  *
  7468.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  7469.  */
  7470. static U_CHAR *
  7471. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  7472.      register U_CHAR *bp;
  7473.      register U_CHAR *limit;
  7474.      int start_line;
  7475.      int *count_newlines;
  7476.      int *backslash_newlines_p;
  7477.      int *eofp;
  7478. {
  7479.   register U_CHAR c, match;
  7480.  
  7481.   match = *bp++;
  7482.   while (1) {
  7483.     if (bp >= limit) {
  7484.       error_with_line (line_for_error (start_line),
  7485.                "unterminated string or character constant");
  7486.       error_with_line (multiline_string_line,
  7487.                "possible real start of unterminated constant");
  7488.       multiline_string_line = 0;
  7489.       if (eofp)
  7490.     *eofp = 1;
  7491.       break;
  7492.     }
  7493.     c = *bp++;
  7494.     if (c == '\\') {
  7495.       while (*bp == '\\' && bp[1] == '\n') {
  7496.     if (backslash_newlines_p)
  7497.       *backslash_newlines_p = 1;
  7498.     if (count_newlines)
  7499.       ++*count_newlines;
  7500.     bp += 2;
  7501.       }
  7502.       if (*bp == '\n' && count_newlines) {
  7503.     if (backslash_newlines_p)
  7504.       *backslash_newlines_p = 1;
  7505.     ++*count_newlines;
  7506.       }
  7507.       bp++;
  7508.     } else if (c == '\n') {
  7509.       if (traditional) {
  7510.      /* Unterminated strings and character constants are 'legal'.  */
  7511.      bp--;    /* Don't consume the newline. */
  7512.      if (eofp)
  7513.        *eofp = 1;
  7514.      break;
  7515.       }
  7516.       if (pedantic || match == '\'') {
  7517.     error_with_line (line_for_error (start_line),
  7518.              "unterminated string or character constant");
  7519.     bp--;
  7520.     if (eofp)
  7521.       *eofp = 1;
  7522.     break;
  7523.       }
  7524.       /* If not traditional, then allow newlines inside strings.  */
  7525.       if (count_newlines)
  7526.     ++*count_newlines;
  7527.       if (multiline_string_line == 0)
  7528.     multiline_string_line = start_line;
  7529.     } else if (c == match)
  7530.       break;
  7531.   }
  7532.   return bp;
  7533. }
  7534.  
  7535. /* Place into DST a quoted string representing the string SRC.
  7536.    Return the address of DST's terminating null.  */
  7537. static char *
  7538. quote_string (dst, src)
  7539.      char *dst, *src;
  7540. {
  7541.   U_CHAR c;
  7542.  
  7543.   *dst++ = '\"';
  7544.   for (;;)
  7545.     switch ((c = *src++))
  7546.       {
  7547.       default:
  7548.         if (isprint (c))
  7549.       *dst++ = c;
  7550.     else
  7551.       {
  7552.         sprintf (dst, "\\%03o", c);
  7553.         dst += 4;
  7554.       }
  7555.     break;
  7556.  
  7557.       case '\"':
  7558.       case '\\':
  7559.     *dst++ = '\\';
  7560.     *dst++ = c;
  7561.     break;
  7562.       
  7563.       case '\0':
  7564.     *dst++ = '\"';
  7565.     *dst = '\0';
  7566.     return dst;
  7567.       }
  7568. }
  7569.  
  7570. /* Skip across a group of balanced parens, starting from IP->bufp.
  7571.    IP->bufp is updated.  Use this with IP->bufp pointing at an open-paren.
  7572.  
  7573.    This does not handle newlines, because it's used for the arg of #if,
  7574.    where there aren't any newlines.  Also, backslash-newline can't appear.  */
  7575.  
  7576. static U_CHAR *
  7577. skip_paren_group (ip)
  7578.      register FILE_BUF *ip;
  7579. {
  7580.   U_CHAR *limit = ip->buf + ip->length;
  7581.   U_CHAR *p = ip->bufp;
  7582.   int depth = 0;
  7583.   int lines_dummy = 0;
  7584.  
  7585.   while (p != limit) {
  7586.     int c = *p++;
  7587.     switch (c) {
  7588.     case '(':
  7589.       depth++;
  7590.       break;
  7591.  
  7592.     case ')':
  7593.       depth--;
  7594.       if (depth == 0)
  7595.     return ip->bufp = p;
  7596.       break;
  7597.  
  7598.     case '/':
  7599.       if (*p == '*') {
  7600.     ip->bufp = p;
  7601.     p = skip_to_end_of_comment (ip, &lines_dummy, 0);
  7602.     p = ip->bufp;
  7603.       }
  7604.  
  7605.     case '"':
  7606.     case '\'':
  7607.       {
  7608.     int eofp = 0;
  7609.     p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  7610.     if (eofp)
  7611.       return ip->bufp = p;
  7612.       }
  7613.       break;
  7614.     }
  7615.   }
  7616.  
  7617.   ip->bufp = p;
  7618.   return p;
  7619. }
  7620.  
  7621. /*
  7622.  * write out a #line command, for instance, after an #include file.
  7623.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  7624.  * appear to be a no-op, and we can output a few newlines instead
  7625.  * if we want to increase the line number by a small amount.
  7626.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  7627.  */
  7628.  
  7629. static void
  7630. output_line_command (ip, op, conditional, file_change)
  7631.      FILE_BUF *ip, *op;
  7632.      int conditional;
  7633.      enum file_change_code file_change;
  7634. {
  7635.   int len;
  7636.   char *line_cmd_buf, *line_end;
  7637.  
  7638.   if (no_line_commands
  7639.       || ip->fname == NULL
  7640.       || no_output) {
  7641.     op->lineno = ip->lineno;
  7642.     return;
  7643.   }
  7644.  
  7645.   if (conditional) {
  7646.     if (ip->lineno == op->lineno)
  7647.       return;
  7648.  
  7649.     /* If the inherited line number is a little too small,
  7650.        output some newlines instead of a #line command.  */
  7651.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  7652.       check_expand (op, 10);
  7653.       while (ip->lineno > op->lineno) {
  7654.     *op->bufp++ = '\n';
  7655.     op->lineno++;
  7656.       }
  7657.       return;
  7658.     }
  7659.   }
  7660.  
  7661.   /* Don't output a line number of 0 if we can help it.  */
  7662.   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
  7663.       && *ip->bufp == '\n') {
  7664.     ip->lineno++;
  7665.     ip->bufp++;
  7666.   }
  7667.  
  7668.   line_cmd_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100);
  7669. #ifdef OUTPUT_LINE_COMMANDS
  7670.   sprintf (line_cmd_buf, "#line %d ", ip->lineno);
  7671. #else
  7672.   sprintf (line_cmd_buf, "# %d ", ip->lineno);
  7673. #endif
  7674. /* An AMB Hack START, all new code. */
  7675.   if(ip->dir && ip->dir->fname)
  7676.      line_end = quote_string (line_cmd_buf + strlen (line_cmd_buf),
  7677.                               &ip->nominal_fname[strlen(ip->dir->fname)+1]);
  7678.   else
  7679. /* An AMB Hack END */
  7680.      line_end = quote_string (line_cmd_buf + strlen (line_cmd_buf),
  7681.                               ip->nominal_fname);
  7682.   if (file_change != same_file) {
  7683.     *line_end++ = ' ';
  7684.     *line_end++ = file_change == enter_file ? '1' : '2';
  7685.   }
  7686.   /* Tell cc1 if following text comes from a system header file.  */
  7687.   if (ip->system_header_p) {
  7688.     *line_end++ = ' ';
  7689.     *line_end++ = '3';
  7690.   }
  7691. #ifndef NO_IMPLICIT_EXTERN_C
  7692.   /* Tell cc1plus if following text should be treated as C.  */
  7693.   if (ip->system_header_p == 2 && cplusplus) {
  7694.     *line_end++ = ' ';
  7695.     *line_end++ = '4';
  7696.   }
  7697. #endif
  7698.   *line_end++ = '\n';
  7699.   len = line_end - line_cmd_buf;
  7700.   check_expand (op, len + 1);
  7701.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7702.     *op->bufp++ = '\n';
  7703.   bcopy ((char *) line_cmd_buf, (char *) op->bufp, len);
  7704.   op->bufp += len;
  7705.   op->lineno = ip->lineno;
  7706. }
  7707.  
  7708. /* This structure represents one parsed argument in a macro call.
  7709.    `raw' points to the argument text as written (`raw_length' is its length).
  7710.    `expanded' points to the argument's macro-expansion
  7711.    (its length is `expand_length').
  7712.    `stringified_length' is the length the argument would have
  7713.    if stringified.
  7714.    `use_count' is the number of times this macro arg is substituted
  7715.    into the macro.  If the actual use count exceeds 10, 
  7716.    the value stored is 10.
  7717.    `free1' and `free2', if nonzero, point to blocks to be freed
  7718.    when the macro argument data is no longer needed.  */
  7719.  
  7720. struct argdata {
  7721.   U_CHAR *raw, *expanded;
  7722.   int raw_length, expand_length;
  7723.   int stringified_length;
  7724.   U_CHAR *free1, *free2;
  7725.   char newlines;
  7726.   char comments;
  7727.   char use_count;
  7728. };
  7729.  
  7730. /* Expand a macro call.
  7731.    HP points to the symbol that is the macro being called.
  7732.    Put the result of expansion onto the input stack
  7733.    so that subsequent input by our caller will use it.
  7734.  
  7735.    If macro wants arguments, caller has already verified that
  7736.    an argument list follows; arguments come from the input stack.  */
  7737.  
  7738. static void
  7739. macroexpand (hp, op)
  7740.      HASHNODE *hp;
  7741.      FILE_BUF *op;
  7742. {
  7743.   int nargs;
  7744.   DEFINITION *defn = hp->value.defn;
  7745.   register U_CHAR *xbuf;
  7746.   int xbuf_len;
  7747.   int start_line = instack[indepth].lineno;
  7748.   int rest_args, rest_zero;
  7749.  
  7750.   CHECK_DEPTH (return;);
  7751.  
  7752.   /* it might not actually be a macro.  */
  7753.   if (hp->type != T_MACRO) {
  7754.     special_symbol (hp, op);
  7755.     return;
  7756.   }
  7757.  
  7758.   /* This macro is being used inside a #if, which means it must be */
  7759.   /* recorded as a precondition.  */
  7760.   if (pcp_inside_if && pcp_outfile && defn->predefined)
  7761.     dump_single_macro (hp, pcp_outfile);
  7762.   
  7763.   nargs = defn->nargs;
  7764.  
  7765.   if (nargs >= 0) {
  7766.     register int i;
  7767.     struct argdata *args;
  7768.     char *parse_error = 0;
  7769.  
  7770.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  7771.  
  7772.     for (i = 0; i < nargs; i++) {
  7773.       args[i].raw = (U_CHAR *) "";
  7774.       args[i].expanded = 0;
  7775.       args[i].raw_length = args[i].expand_length
  7776.     = args[i].stringified_length = 0;
  7777.       args[i].free1 = args[i].free2 = 0;
  7778.       args[i].use_count = 0;
  7779.     }
  7780.  
  7781.     /* Parse all the macro args that are supplied.  I counts them.
  7782.        The first NARGS args are stored in ARGS.
  7783.        The rest are discarded.
  7784.        If rest_args is set then we assume macarg absorbed the rest of the args.
  7785.        */
  7786.     i = 0;
  7787.     rest_args = 0;
  7788.     do {
  7789.       /* Discard the open-parenthesis or comma before the next arg.  */
  7790.       ++instack[indepth].bufp;
  7791.       if (rest_args)
  7792.     continue;
  7793.       if (i < nargs || (nargs == 0 && i == 0)) {
  7794.     /* if we are working on last arg which absorbs rest of args... */
  7795.     if (i == nargs - 1 && defn->rest_args)
  7796.       rest_args = 1;
  7797.     parse_error = macarg (&args[i], rest_args);
  7798.       }
  7799.       else
  7800.     parse_error = macarg (NULL_PTR, 0);
  7801.       if (parse_error) {
  7802.     error_with_line (line_for_error (start_line), parse_error);
  7803.     break;
  7804.       }
  7805.       i++;
  7806.     } while (*instack[indepth].bufp != ')');
  7807.  
  7808.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  7809.     if (i == 1) {
  7810.       register U_CHAR *bp = args[0].raw;
  7811.       register U_CHAR *lim = bp + args[0].raw_length;
  7812.       /* cpp.texi says for foo ( ) we provide one argument.
  7813.      However, if foo wants just 0 arguments, treat this as 0.  */
  7814.       if (nargs == 0)
  7815.     while (bp != lim && is_space[*bp]) bp++;
  7816.       if (bp == lim)
  7817.     i = 0;
  7818.     }
  7819.  
  7820.     /* Don't output an error message if we have already output one for
  7821.        a parse error above.  */
  7822.     rest_zero = 0;
  7823.     if (nargs == 0 && i > 0) {
  7824.       if (! parse_error)
  7825.     error ("arguments given to macro `%s'", hp->name);
  7826.     } else if (i < nargs) {
  7827.       /* traditional C allows foo() if foo wants one argument.  */
  7828.       if (nargs == 1 && i == 0 && traditional)
  7829.     ;
  7830.       /* the rest args token is allowed to absorb 0 tokens */
  7831.       else if (i == nargs - 1 && defn->rest_args)
  7832.     rest_zero = 1;
  7833.       else if (parse_error)
  7834.     ;
  7835.       else if (i == 0)
  7836.     error ("macro `%s' used without args", hp->name);
  7837.       else if (i == 1)
  7838.     error ("macro `%s' used with just one arg", hp->name);
  7839.       else
  7840.     error ("macro `%s' used with only %d args", hp->name, i);
  7841.     } else if (i > nargs) {
  7842.       if (! parse_error)
  7843.     error ("macro `%s' used with too many (%d) args", hp->name, i);
  7844.     }
  7845.  
  7846.     /* Swallow the closeparen.  */
  7847.     ++instack[indepth].bufp;
  7848.  
  7849.     /* If macro wants zero args, we parsed the arglist for checking only.
  7850.        Read directly from the macro definition.  */
  7851.     if (nargs == 0) {
  7852.       xbuf = defn->expansion;
  7853.       xbuf_len = defn->length;
  7854.     } else {
  7855.       register U_CHAR *exp = defn->expansion;
  7856.       register int offset;    /* offset in expansion,
  7857.                    copied a piece at a time */
  7858.       register int totlen;    /* total amount of exp buffer filled so far */
  7859.  
  7860.       register struct reflist *ap, *last_ap;
  7861.  
  7862.       /* Macro really takes args.  Compute the expansion of this call.  */
  7863.  
  7864.       /* Compute length in characters of the macro's expansion.
  7865.      Also count number of times each arg is used.  */
  7866.       xbuf_len = defn->length;
  7867.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  7868.     if (ap->stringify)
  7869.       xbuf_len += args[ap->argno].stringified_length;
  7870.     else if (ap->raw_before || ap->raw_after || traditional)
  7871.       /* Add 4 for two newline-space markers to prevent
  7872.          token concatenation.  */
  7873.       xbuf_len += args[ap->argno].raw_length + 4;
  7874.     else {
  7875.       /* We have an ordinary (expanded) occurrence of the arg.
  7876.          So compute its expansion, if we have not already.  */
  7877.       if (args[ap->argno].expanded == 0) {
  7878.         FILE_BUF obuf;
  7879.         obuf = expand_to_temp_buffer (args[ap->argno].raw,
  7880.                       args[ap->argno].raw + args[ap->argno].raw_length,
  7881.                       1, 0);
  7882.  
  7883.         args[ap->argno].expanded = obuf.buf;
  7884.         args[ap->argno].expand_length = obuf.length;
  7885.         args[ap->argno].free2 = obuf.buf;
  7886.       }
  7887.  
  7888.       /* Add 4 for two newline-space markers to prevent
  7889.          token concatenation.  */
  7890.       xbuf_len += args[ap->argno].expand_length + 4;
  7891.     }
  7892.     if (args[ap->argno].use_count < 10)
  7893.       args[ap->argno].use_count++;
  7894.       }
  7895.  
  7896.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  7897.  
  7898.       /* Generate in XBUF the complete expansion
  7899.      with arguments substituted in.
  7900.      TOTLEN is the total size generated so far.
  7901.      OFFSET is the index in the definition
  7902.      of where we are copying from.  */
  7903.       offset = totlen = 0;
  7904.       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
  7905.        last_ap = ap, ap = ap->next) {
  7906.     register struct argdata *arg = &args[ap->argno];
  7907.     int count_before = totlen;
  7908.  
  7909.     /* Add chars to XBUF.  */
  7910.     for (i = 0; i < ap->nchars; i++, offset++)
  7911.       xbuf[totlen++] = exp[offset];
  7912.  
  7913.     /* If followed by an empty rest arg with concatenation,
  7914.        delete the last run of nonwhite chars.  */
  7915.     if (rest_zero && totlen > count_before
  7916.         && ((ap->rest_args && ap->raw_before)
  7917.         || (last_ap != NULL && last_ap->rest_args
  7918.             && last_ap->raw_after))) {
  7919.       /* Delete final whitespace.  */
  7920.       while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
  7921.         totlen--;
  7922.       }
  7923.  
  7924.       /* Delete the nonwhites before them.  */
  7925.       while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
  7926.         totlen--;
  7927.       }
  7928.     }
  7929.  
  7930.     if (ap->stringify != 0) {
  7931.       int arglen = arg->raw_length;
  7932.       int escaped = 0;
  7933.       int in_string = 0;
  7934.       int c;
  7935.       i = 0;
  7936.       while (i < arglen
  7937.          && (c = arg->raw[i], is_space[c]))
  7938.         i++;
  7939.       while (i < arglen
  7940.          && (c = arg->raw[arglen - 1], is_space[c]))
  7941.         arglen--;
  7942.       if (!traditional)
  7943.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  7944.       for (; i < arglen; i++) {
  7945.         c = arg->raw[i];
  7946.  
  7947.         /* Special markers Newline Space
  7948.            generate nothing for a stringified argument.  */
  7949.         if (c == '\n' && arg->raw[i+1] != '\n') {
  7950.           i++;
  7951.           continue;
  7952.         }
  7953.  
  7954.         /* Internal sequences of whitespace are replaced by one space
  7955.            except within an string or char token.  */
  7956.         if (! in_string
  7957.         && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
  7958.           while (1) {
  7959.         /* Note that Newline Space does occur within whitespace
  7960.            sequences; consider it part of the sequence.  */
  7961.         if (c == '\n' && is_space[arg->raw[i+1]])
  7962.           i += 2;
  7963.         else if (c != '\n' && is_space[c])
  7964.           i++;
  7965.         else break;
  7966.         c = arg->raw[i];
  7967.           }
  7968.           i--;
  7969.           c = ' ';
  7970.         }
  7971.  
  7972.         if (escaped)
  7973.           escaped = 0;
  7974.         else {
  7975.           if (c == '\\')
  7976.         escaped = 1;
  7977.           if (in_string) {
  7978.         if (c == in_string)
  7979.           in_string = 0;
  7980.           } else if (c == '\"' || c == '\'')
  7981.         in_string = c;
  7982.         }
  7983.  
  7984.         /* Escape these chars */
  7985.         if (c == '\"' || (in_string && c == '\\'))
  7986.           xbuf[totlen++] = '\\';
  7987.         if (isprint (c))
  7988.           xbuf[totlen++] = c;
  7989.         else {
  7990.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  7991.           totlen += 4;
  7992.         }
  7993.       }
  7994.       if (!traditional)
  7995.         xbuf[totlen++] = '\"'; /* insert ending quote */
  7996.     } else if (ap->raw_before || ap->raw_after || traditional) {
  7997.       U_CHAR *p1 = arg->raw;
  7998.       U_CHAR *l1 = p1 + arg->raw_length;
  7999.       if (ap->raw_before) {
  8000.         while (p1 != l1 && is_space[*p1]) p1++;
  8001.         while (p1 != l1 && is_idchar[*p1])
  8002.           xbuf[totlen++] = *p1++;
  8003.         /* Delete any no-reexpansion marker that follows
  8004.            an identifier at the beginning of the argument
  8005.            if the argument is concatenated with what precedes it.  */
  8006.         if (p1[0] == '\n' && p1[1] == '-')
  8007.           p1 += 2;
  8008.       } else if (!traditional) {
  8009.       /* Ordinary expanded use of the argument.
  8010.          Put in newline-space markers to prevent token pasting.  */
  8011.         xbuf[totlen++] = '\n';
  8012.         xbuf[totlen++] = ' ';
  8013.       }
  8014.       if (ap->raw_after) {
  8015.         /* Arg is concatenated after: delete trailing whitespace,
  8016.            whitespace markers, and no-reexpansion markers.  */
  8017.         while (p1 != l1) {
  8018.           if (is_space[l1[-1]]) l1--;
  8019.           else if (l1[-1] == '-') {
  8020.         U_CHAR *p2 = l1 - 1;
  8021.         /* If a `-' is preceded by an odd number of newlines then it
  8022.            and the last newline are a no-reexpansion marker.  */
  8023.         while (p2 != p1 && p2[-1] == '\n') p2--;
  8024.         if ((l1 - 1 - p2) & 1) {
  8025.           l1 -= 2;
  8026.         }
  8027.         else break;
  8028.           }
  8029.           else break;
  8030.         }
  8031.       }
  8032.  
  8033.       bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
  8034.       totlen += l1 - p1;
  8035.       if (!traditional && !ap->raw_after) {
  8036.         /* Ordinary expanded use of the argument.
  8037.            Put in newline-space markers to prevent token pasting.  */
  8038.         xbuf[totlen++] = '\n';
  8039.         xbuf[totlen++] = ' ';
  8040.       }
  8041.     } else {
  8042.       /* Ordinary expanded use of the argument.
  8043.          Put in newline-space markers to prevent token pasting.  */
  8044.       if (!traditional) {
  8045.         xbuf[totlen++] = '\n';
  8046.         xbuf[totlen++] = ' ';
  8047.       }
  8048.       bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
  8049.          arg->expand_length);
  8050.       totlen += arg->expand_length;
  8051.       if (!traditional) {
  8052.         xbuf[totlen++] = '\n';
  8053.         xbuf[totlen++] = ' ';
  8054.       }
  8055.       /* If a macro argument with newlines is used multiple times,
  8056.          then only expand the newlines once.  This avoids creating output
  8057.          lines which don't correspond to any input line, which confuses
  8058.          gdb and gcov.  */
  8059.       if (arg->use_count > 1 && arg->newlines > 0) {
  8060.         /* Don't bother doing change_newlines for subsequent
  8061.            uses of arg.  */
  8062.         arg->use_count = 1;
  8063.         arg->expand_length
  8064.           = change_newlines (arg->expanded, arg->expand_length);
  8065.       }
  8066.     }
  8067.  
  8068.     if (totlen > xbuf_len)
  8069.       abort ();
  8070.       }
  8071.  
  8072.       /* if there is anything left of the definition
  8073.      after handling the arg list, copy that in too. */
  8074.  
  8075.       for (i = offset; i < defn->length; i++) {
  8076.     /* if we've reached the end of the macro */
  8077.     if (exp[i] == ')')
  8078.       rest_zero = 0;
  8079.     if (! (rest_zero && last_ap != NULL && last_ap->rest_args
  8080.            && last_ap->raw_after))
  8081.       xbuf[totlen++] = exp[i];
  8082.       }
  8083.  
  8084.       xbuf[totlen] = 0;
  8085.       xbuf_len = totlen;
  8086.  
  8087.       for (i = 0; i < nargs; i++) {
  8088.     if (args[i].free1 != 0)
  8089.       free (args[i].free1);
  8090.     if (args[i].free2 != 0)
  8091.       free (args[i].free2);
  8092.       }
  8093.     }
  8094.   } else {
  8095.     xbuf = defn->expansion;
  8096.     xbuf_len = defn->length;
  8097.   }
  8098.  
  8099.   /* Now put the expansion on the input stack
  8100.      so our caller will commence reading from it.  */
  8101.   {
  8102.     register FILE_BUF *ip2;
  8103.  
  8104.     ip2 = &instack[++indepth];
  8105.  
  8106.     ip2->fname = 0;
  8107.     ip2->nominal_fname = 0;
  8108.     /* This may not be exactly correct, but will give much better error
  8109.        messages for nested macro calls than using a line number of zero.  */
  8110.     ip2->lineno = start_line;
  8111.     ip2->buf = xbuf;
  8112.     ip2->length = xbuf_len;
  8113.     ip2->bufp = xbuf;
  8114.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  8115.     ip2->macro = hp;
  8116.     ip2->if_stack = if_stack;
  8117.     ip2->system_header_p = 0;
  8118.  
  8119.     /* Recursive macro use sometimes works traditionally.
  8120.        #define foo(x,y) bar (x (y,0), y)
  8121.        foo (foo, baz)  */
  8122.  
  8123.     if (!traditional)
  8124.       hp->type = T_DISABLED;
  8125.   }
  8126. }
  8127.  
  8128. /*
  8129.  * Parse a macro argument and store the info on it into *ARGPTR.
  8130.  * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
  8131.  * Return nonzero to indicate a syntax error.
  8132.  */
  8133.  
  8134. static char *
  8135. macarg (argptr, rest_args)
  8136.      register struct argdata *argptr;
  8137.      int rest_args;
  8138. {
  8139.   FILE_BUF *ip = &instack[indepth];
  8140.   int paren = 0;
  8141.   int newlines = 0;
  8142.   int comments = 0;
  8143.  
  8144.   /* Try to parse as much of the argument as exists at this
  8145.      input stack level.  */
  8146.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  8147.             &paren, &newlines, &comments, rest_args);
  8148.  
  8149.   /* If we find the end of the argument at this level,
  8150.      set up *ARGPTR to point at it in the input stack.  */
  8151.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  8152.       && bp != ip->buf + ip->length) {
  8153.     if (argptr != 0) {
  8154.       argptr->raw = ip->bufp;
  8155.       argptr->raw_length = bp - ip->bufp;
  8156.       argptr->newlines = newlines;
  8157.     }
  8158.     ip->bufp = bp;
  8159.   } else {
  8160.     /* This input stack level ends before the macro argument does.
  8161.        We must pop levels and keep parsing.
  8162.        Therefore, we must allocate a temporary buffer and copy
  8163.        the macro argument into it.  */
  8164.     int bufsize = bp - ip->bufp;
  8165.     int extra = newlines;
  8166.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  8167.     int final_start = 0;
  8168.  
  8169.     bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
  8170.     ip->bufp = bp;
  8171.     ip->lineno += newlines;
  8172.  
  8173.     while (bp == ip->buf + ip->length) {
  8174.       if (instack[indepth].macro == 0) {
  8175.     free (buffer);
  8176.     return "unterminated macro call";
  8177.       }
  8178.       ip->macro->type = T_MACRO;
  8179.       if (ip->free_ptr)
  8180.     free (ip->free_ptr);
  8181.       ip = &instack[--indepth];
  8182.       newlines = 0;
  8183.       comments = 0;
  8184.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  8185.             &newlines, &comments, rest_args);
  8186.       final_start = bufsize;
  8187.       bufsize += bp - ip->bufp;
  8188.       extra += newlines;
  8189.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  8190.       bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
  8191.          bp - ip->bufp);
  8192.       ip->bufp = bp;
  8193.       ip->lineno += newlines;
  8194.     }
  8195.  
  8196.     /* Now, if arg is actually wanted, record its raw form,
  8197.        discarding comments and duplicating newlines in whatever
  8198.        part of it did not come from a macro expansion.
  8199.        EXTRA space has been preallocated for duplicating the newlines.
  8200.        FINAL_START is the index of the start of that part.  */
  8201.     if (argptr != 0) {
  8202.       argptr->raw = buffer;
  8203.       argptr->raw_length = bufsize;
  8204.       argptr->free1 = buffer;
  8205.       argptr->newlines = newlines;
  8206.       argptr->comments = comments;
  8207.       if ((newlines || comments) && ip->fname != 0)
  8208.     argptr->raw_length
  8209.       = final_start +
  8210.         discard_comments (argptr->raw + final_start,
  8211.                   argptr->raw_length - final_start,
  8212.                   newlines);
  8213.       argptr->raw[argptr->raw_length] = 0;
  8214.       if (argptr->raw_length > bufsize + extra)
  8215.     abort ();
  8216.     }
  8217.   }
  8218.  
  8219.   /* If we are not discarding this argument,
  8220.      macroexpand it and compute its length as stringified.
  8221.      All this info goes into *ARGPTR.  */
  8222.  
  8223.   if (argptr != 0) {
  8224.     register U_CHAR *buf, *lim;
  8225.     register int totlen;
  8226.  
  8227.     buf = argptr->raw;
  8228.     lim = buf + argptr->raw_length;
  8229.  
  8230.     while (buf != lim && is_space[*buf])
  8231.       buf++;
  8232.     while (buf != lim && is_space[lim[-1]])
  8233.       lim--;
  8234.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  8235.     while (buf != lim) {
  8236.       register U_CHAR c = *buf++;
  8237.       totlen++;
  8238.       /* Internal sequences of whitespace are replaced by one space
  8239.      in most cases, but not always.  So count all the whitespace
  8240.      in case we need to keep it all.  */
  8241. #if 0
  8242.       if (is_space[c])
  8243.     SKIP_ALL_WHITE_SPACE (buf);
  8244.       else
  8245. #endif
  8246.       if (c == '\"' || c == '\\') /* escape these chars */
  8247.     totlen++;
  8248.       else if (!isprint (c))
  8249.     totlen += 3;
  8250.     }
  8251.     argptr->stringified_length = totlen;
  8252.   }
  8253.   return 0;
  8254. }
  8255.  
  8256. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  8257.    counting parens in *DEPTHPTR,
  8258.    and return if reach LIMIT
  8259.    or before a `)' that would make *DEPTHPTR negative
  8260.    or before a comma when *DEPTHPTR is zero.
  8261.    Single and double quotes are matched and termination
  8262.    is inhibited within them.  Comments also inhibit it.
  8263.    Value returned is pointer to stopping place.
  8264.  
  8265.    Increment *NEWLINES each time a newline is passed.
  8266.    REST_ARGS notifies macarg1 that it should absorb the rest of the args.
  8267.    Set *COMMENTS to 1 if a comment is seen.  */
  8268.  
  8269. static U_CHAR *
  8270. macarg1 (start, limit, depthptr, newlines, comments, rest_args)
  8271.      U_CHAR *start;
  8272.      register U_CHAR *limit;
  8273.      int *depthptr, *newlines, *comments;
  8274.      int rest_args;
  8275. {
  8276.   register U_CHAR *bp = start;
  8277.  
  8278.   while (bp < limit) {
  8279.     switch (*bp) {
  8280.     case '(':
  8281.       (*depthptr)++;
  8282.       break;
  8283.     case ')':
  8284.       if (--(*depthptr) < 0)
  8285.     return bp;
  8286.       break;
  8287.     case '\\':
  8288.       /* Traditionally, backslash makes following char not special.  */
  8289.       if (bp + 1 < limit && traditional)
  8290.     {
  8291.       bp++;
  8292.       /* But count source lines anyway.  */
  8293.       if (*bp == '\n')
  8294.         ++*newlines;
  8295.     }
  8296.       break;
  8297.     case '\n':
  8298.       ++*newlines;
  8299.       break;
  8300.     case '/':
  8301.       if (bp[1] == '\\' && bp[2] == '\n')
  8302.     newline_fix (bp + 1);
  8303.       if (cplusplus_comments && bp[1] == '/') {
  8304.     *comments = 1;
  8305.     bp += 2;
  8306.     while (bp < limit && (*bp != '\n' || bp[-1] == '\\')) {
  8307.       if (*bp == '\n') ++*newlines;
  8308.       bp++;
  8309.     }
  8310.     break;
  8311.       }
  8312.       if (bp[1] != '*' || bp + 1 >= limit)
  8313.     break;
  8314.       *comments = 1;
  8315.       bp += 2;
  8316.       while (bp + 1 < limit) {
  8317.     if (bp[0] == '*'
  8318.         && bp[1] == '\\' && bp[2] == '\n')
  8319.       newline_fix (bp + 1);
  8320.     if (bp[0] == '*' && bp[1] == '/')
  8321.       break;
  8322.     if (*bp == '\n') ++*newlines;
  8323.     bp++;
  8324.       }
  8325.       break;
  8326.     case '\'':
  8327.     case '\"':
  8328.       {
  8329.     int quotec;
  8330.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  8331.       if (*bp == '\\') {
  8332.         bp++;
  8333.         if (*bp == '\n')
  8334.           ++*newlines;
  8335.         while (*bp == '\\' && bp[1] == '\n') {
  8336.           bp += 2;
  8337.         }
  8338.       } else if (*bp == '\n') {
  8339.         ++*newlines;
  8340.         if (quotec == '\'')
  8341.           break;
  8342.       }
  8343.     }
  8344.       }
  8345.       break;
  8346.     case ',':
  8347.       /* if we've returned to lowest level and we aren't absorbing all args */
  8348.       if ((*depthptr) == 0 && rest_args == 0)
  8349.     return bp;
  8350.       break;
  8351.     }
  8352.     bp++;
  8353.   }
  8354.  
  8355.   return bp;
  8356. }
  8357.  
  8358. /* Discard comments and duplicate newlines
  8359.    in the string of length LENGTH at START,
  8360.    except inside of string constants.
  8361.    The string is copied into itself with its beginning staying fixed.  
  8362.  
  8363.    NEWLINES is the number of newlines that must be duplicated.
  8364.    We assume that that much extra space is available past the end
  8365.    of the string.  */
  8366.  
  8367. static int
  8368. discard_comments (start, length, newlines)
  8369.      U_CHAR *start;
  8370.      int length;
  8371.      int newlines;
  8372. {
  8373.   register U_CHAR *ibp;
  8374.   register U_CHAR *obp;
  8375.   register U_CHAR *limit;
  8376.   register int c;
  8377.  
  8378.   /* If we have newlines to duplicate, copy everything
  8379.      that many characters up.  Then, in the second part,
  8380.      we will have room to insert the newlines
  8381.      while copying down.
  8382.      NEWLINES may actually be too large, because it counts
  8383.      newlines in string constants, and we don't duplicate those.
  8384.      But that does no harm.  */
  8385.   if (newlines > 0) {
  8386.     ibp = start + length;
  8387.     obp = ibp + newlines;
  8388.     limit = start;
  8389.     while (limit != ibp)
  8390.       *--obp = *--ibp;
  8391.   }
  8392.  
  8393.   ibp = start + newlines;
  8394.   limit = start + length + newlines;
  8395.   obp = start;
  8396.  
  8397.   while (ibp < limit) {
  8398.     *obp++ = c = *ibp++;
  8399.     switch (c) {
  8400.     case '\n':
  8401.       /* Duplicate the newline.  */
  8402.       *obp++ = '\n';
  8403.       break;
  8404.  
  8405.     case '\\':
  8406.       if (*ibp == '\n') {
  8407.     obp--;
  8408.     ibp++;
  8409.       }
  8410.       break;
  8411.  
  8412.     case '/':
  8413.       if (*ibp == '\\' && ibp[1] == '\n')
  8414.     newline_fix (ibp);
  8415.       /* Delete any comment.  */
  8416.       if (cplusplus_comments && ibp[0] == '/') {
  8417.     /* Comments are equivalent to spaces.  */
  8418.     obp[-1] = ' ';
  8419.     ibp++;
  8420.     while (ibp < limit && (*ibp != '\n' || ibp[-1] == '\\'))
  8421.       ibp++;
  8422.     break;
  8423.       }
  8424.       if (ibp[0] != '*' || ibp + 1 >= limit)
  8425.     break;
  8426.       /* Comments are equivalent to spaces.  */
  8427.       obp[-1] = ' ';
  8428.       ibp++;
  8429.       while (ibp + 1 < limit) {
  8430.     if (ibp[0] == '*'
  8431.         && ibp[1] == '\\' && ibp[2] == '\n')
  8432.       newline_fix (ibp + 1);
  8433.     if (ibp[0] == '*' && ibp[1] == '/')
  8434.       break;
  8435.     ibp++;
  8436.       }
  8437.       ibp += 2;
  8438.       break;
  8439.  
  8440.     case '\'':
  8441.     case '\"':
  8442.       /* Notice and skip strings, so that we don't
  8443.      think that comments start inside them,
  8444.      and so we don't duplicate newlines in them.  */
  8445.       {
  8446.     int quotec = c;
  8447.     while (ibp < limit) {
  8448.       *obp++ = c = *ibp++;
  8449.       if (c == quotec)
  8450.         break;
  8451.       if (c == '\n' && quotec == '\'')
  8452.         break;
  8453.       if (c == '\\' && ibp < limit) {
  8454.         while (*ibp == '\\' && ibp[1] == '\n')
  8455.           ibp += 2;
  8456.         *obp++ = *ibp++;
  8457.       }
  8458.     }
  8459.       }
  8460.       break;
  8461.     }
  8462.   }
  8463.  
  8464.   return obp - start;
  8465. }
  8466.  
  8467. /* Turn newlines to spaces in the string of length LENGTH at START,
  8468.    except inside of string constants.
  8469.    The string is copied into itself with its beginning staying fixed.  */
  8470.  
  8471. static int
  8472. change_newlines (start, length)
  8473.      U_CHAR *start;
  8474.      int length;
  8475. {
  8476.   register U_CHAR *ibp;
  8477.   register U_CHAR *obp;
  8478.   register U_CHAR *limit;
  8479.   register int c;
  8480.  
  8481.   ibp = start;
  8482.   limit = start + length;
  8483.   obp = start;
  8484.  
  8485.   while (ibp < limit) {
  8486.     *obp++ = c = *ibp++;
  8487.     switch (c) {
  8488.     case '\n':
  8489.       /* If this is a NEWLINE NEWLINE, then this is a real newline in the
  8490.      string.  Skip past the newline and its duplicate.
  8491.      Put a space in the output.  */
  8492.       if (*ibp == '\n')
  8493.     {
  8494.       ibp++;
  8495.       obp--;
  8496.       *obp++ = ' ';
  8497.     }
  8498.       break;
  8499.  
  8500.     case '\'':
  8501.     case '\"':
  8502.       /* Notice and skip strings, so that we don't delete newlines in them.  */
  8503.       {
  8504.     int quotec = c;
  8505.     while (ibp < limit) {
  8506.       *obp++ = c = *ibp++;
  8507.       if (c == quotec)
  8508.         break;
  8509.       if (c == '\n' && quotec == '\'')
  8510.         break;
  8511.     }
  8512.       }
  8513.       break;
  8514.     }
  8515.   }
  8516.  
  8517.   return obp - start;
  8518. }
  8519.  
  8520. /*
  8521.  * my_strerror - return the descriptive text associated with an `errno' code.
  8522.  */
  8523.  
  8524. char *
  8525. my_strerror (errnum)
  8526.      int errnum;
  8527. {
  8528.   char *result;
  8529.  
  8530. #ifndef VMS
  8531. #ifndef HAVE_STRERROR
  8532.   result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
  8533. #else
  8534.   result = strerror (errnum);
  8535. #endif
  8536. #else    /* VMS */
  8537.   /* VAXCRTL's strerror() takes an optional second argument, which only
  8538.      matters when the first argument is EVMSERR.  However, it's simplest
  8539.      just to pass it unconditionally.  `vaxc$errno' is declared in
  8540.      <errno.h>, and maintained by the library in parallel with `errno'.
  8541.      We assume that caller's `errnum' either matches the last setting of
  8542.      `errno' by the library or else does not have the value `EVMSERR'.  */
  8543.  
  8544.   result = strerror (errnum, vaxc$errno);
  8545. #endif
  8546.  
  8547.   if (!result)
  8548.     result = "undocumented I/O error";
  8549.  
  8550.   return result;
  8551. }
  8552.  
  8553. /*
  8554.  * error - print error message and increment count of errors.
  8555.  */
  8556.  
  8557. void
  8558. error (msg, arg1, arg2, arg3)
  8559.      char *msg;
  8560.      char *arg1, *arg2, *arg3;
  8561. {
  8562.   int i;
  8563.   FILE_BUF *ip = NULL;
  8564.  
  8565.   print_containing_files ();
  8566.  
  8567.   for (i = indepth; i >= 0; i--)
  8568.     if (instack[i].fname != NULL) {
  8569.       ip = &instack[i];
  8570.       break;
  8571.     }
  8572.  
  8573.   if (ip != NULL)
  8574.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8575.   fprintf (stderr, msg, arg1, arg2, arg3);
  8576.   fprintf (stderr, "\n");
  8577.   errors++;
  8578. }
  8579.  
  8580. /* Error including a message from `errno'.  */
  8581.  
  8582. static void
  8583. error_from_errno (name)
  8584.      char *name;
  8585. {
  8586.   int i;
  8587.   FILE_BUF *ip = NULL;
  8588.  
  8589.   print_containing_files ();
  8590.  
  8591.   for (i = indepth; i >= 0; i--)
  8592.     if (instack[i].fname != NULL) {
  8593.       ip = &instack[i];
  8594.       break;
  8595.     }
  8596.  
  8597.   if (ip != NULL)
  8598.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8599.  
  8600.   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
  8601.  
  8602.   errors++;
  8603. }
  8604.  
  8605. /* Print error message but don't count it.  */
  8606.  
  8607. void
  8608. warning (msg, arg1, arg2, arg3)
  8609.      char *msg;
  8610.      char *arg1, *arg2, *arg3;
  8611. {
  8612.   int i;
  8613.   FILE_BUF *ip = NULL;
  8614.  
  8615.   if (inhibit_warnings)
  8616.     return;
  8617.  
  8618.   if (warnings_are_errors)
  8619.     errors++;
  8620.  
  8621.   print_containing_files ();
  8622.  
  8623.   for (i = indepth; i >= 0; i--)
  8624.     if (instack[i].fname != NULL) {
  8625.       ip = &instack[i];
  8626.       break;
  8627.     }
  8628.  
  8629.   if (ip != NULL)
  8630.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8631.   fprintf (stderr, "warning: ");
  8632.   fprintf (stderr, msg, arg1, arg2, arg3);
  8633.   fprintf (stderr, "\n");
  8634. }
  8635.  
  8636. static void
  8637. error_with_line (line, msg, arg1, arg2, arg3)
  8638.      int line;
  8639.      char *msg;
  8640.      char *arg1, *arg2, *arg3;
  8641. {
  8642.   int i;
  8643.   FILE_BUF *ip = NULL;
  8644.  
  8645.   print_containing_files ();
  8646.  
  8647.   for (i = indepth; i >= 0; i--)
  8648.     if (instack[i].fname != NULL) {
  8649.       ip = &instack[i];
  8650.       break;
  8651.     }
  8652.  
  8653.   if (ip != NULL)
  8654.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  8655.   fprintf (stderr, msg, arg1, arg2, arg3);
  8656.   fprintf (stderr, "\n");
  8657.   errors++;
  8658. }
  8659.  
  8660. static void
  8661. warning_with_line (line, msg, arg1, arg2, arg3)
  8662.      int line;
  8663.      char *msg;
  8664.      char *arg1, *arg2, *arg3;
  8665. {
  8666.   int i;
  8667.   FILE_BUF *ip = NULL;
  8668.  
  8669.   if (inhibit_warnings)
  8670.     return;
  8671.  
  8672.   if (warnings_are_errors)
  8673.     errors++;
  8674.  
  8675.   print_containing_files ();
  8676.  
  8677.   for (i = indepth; i >= 0; i--)
  8678.     if (instack[i].fname != NULL) {
  8679.       ip = &instack[i];
  8680.       break;
  8681.     }
  8682.  
  8683.   if (ip != NULL)
  8684.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  8685.   fprintf (stderr, "warning: ");
  8686.   fprintf (stderr, msg, arg1, arg2, arg3);
  8687.   fprintf (stderr, "\n");
  8688. }
  8689.  
  8690. /* print an error message and maybe count it.  */
  8691.  
  8692. void
  8693. pedwarn (msg, arg1, arg2, arg3)
  8694.      char *msg;
  8695.      char *arg1, *arg2, *arg3;
  8696. {
  8697.   if (pedantic_errors)
  8698.     error (msg, arg1, arg2, arg3);
  8699.   else
  8700.     warning (msg, arg1, arg2, arg3);
  8701. }
  8702.  
  8703. void
  8704. pedwarn_with_line (line, msg, arg1, arg2, arg3)
  8705.      int line;
  8706.      char *msg;
  8707.      char *arg1, *arg2, *arg3;
  8708. {
  8709.   if (pedantic_errors)
  8710.     error_with_line (line, msg, arg1, arg2, arg3);
  8711.   else
  8712.     warning_with_line (line, msg, arg1, arg2, arg3);
  8713. }
  8714.  
  8715. /* Report a warning (or an error if pedantic_errors)
  8716.    giving specified file name and line number, not current.  */
  8717.  
  8718. static void
  8719. pedwarn_with_file_and_line (file, line, msg, arg1, arg2, arg3)
  8720.      char *file;
  8721.      int line;
  8722.      char *msg;
  8723.      char *arg1, *arg2, *arg3;
  8724. {
  8725.   if (!pedantic_errors && inhibit_warnings)
  8726.     return;
  8727.   if (file != NULL)
  8728.     fprintf (stderr, "%s:%d: ", file, line);
  8729.   if (pedantic_errors)
  8730.     errors++;
  8731.   if (!pedantic_errors)
  8732.     fprintf (stderr, "warning: ");
  8733.   fprintf (stderr, msg, arg1, arg2, arg3);
  8734.   fprintf (stderr, "\n");
  8735. }
  8736.  
  8737. /* Print the file names and line numbers of the #include
  8738.    commands which led to the current file.  */
  8739.  
  8740. static void
  8741. print_containing_files ()
  8742. {
  8743.   FILE_BUF *ip = NULL;
  8744.   int i;
  8745.   int first = 1;
  8746.  
  8747.   /* If stack of files hasn't changed since we last printed
  8748.      this info, don't repeat it.  */
  8749.   if (last_error_tick == input_file_stack_tick)
  8750.     return;
  8751.  
  8752.   for (i = indepth; i >= 0; i--)
  8753.     if (instack[i].fname != NULL) {
  8754.       ip = &instack[i];
  8755.       break;
  8756.     }
  8757.  
  8758.   /* Give up if we don't find a source file.  */
  8759.   if (ip == NULL)
  8760.     return;
  8761.  
  8762.   /* Find the other, outer source files.  */
  8763.   for (i--; i >= 0; i--)
  8764.     if (instack[i].fname != NULL) {
  8765.       ip = &instack[i];
  8766.       if (first) {
  8767.     first = 0;
  8768.     fprintf (stderr, "In file included");
  8769.       } else {
  8770.     fprintf (stderr, ",\n                ");
  8771.       }
  8772.  
  8773.       fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
  8774.     }
  8775.   if (! first)
  8776.     fprintf (stderr, ":\n");
  8777.  
  8778.   /* Record we have printed the status as of this time.  */
  8779.   last_error_tick = input_file_stack_tick;
  8780. }
  8781.  
  8782. /* Return the line at which an error occurred.
  8783.    The error is not necessarily associated with the current spot
  8784.    in the input stack, so LINE says where.  LINE will have been
  8785.    copied from ip->lineno for the current input level.
  8786.    If the current level is for a file, we return LINE.
  8787.    But if the current level is not for a file, LINE is meaningless.
  8788.    In that case, we return the lineno of the innermost file.  */
  8789.  
  8790. static int
  8791. line_for_error (line)
  8792.      int line;
  8793. {
  8794.   int i;
  8795.   int line1 = line;
  8796.  
  8797.   for (i = indepth; i >= 0; ) {
  8798.     if (instack[i].fname != 0)
  8799.       return line1;
  8800.     i--;
  8801.     if (i < 0)
  8802.       return 0;
  8803.     line1 = instack[i].lineno;
  8804.   }
  8805.   abort ();
  8806.   /*NOTREACHED*/
  8807.   return 0;
  8808. }
  8809.  
  8810. /*
  8811.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  8812.  *
  8813.  * As things stand, nothing is ever placed in the output buffer to be
  8814.  * removed again except when it's KNOWN to be part of an identifier,
  8815.  * so flushing and moving down everything left, instead of expanding,
  8816.  * should work ok.
  8817.  */
  8818.  
  8819. /* You might think void was cleaner for the return type,
  8820.    but that would get type mismatch in check_expand in strict ANSI.  */
  8821. static int
  8822. grow_outbuf (obuf, needed)
  8823.      register FILE_BUF *obuf;
  8824.      register int needed;
  8825. {
  8826.   register U_CHAR *p;
  8827.   int minsize;
  8828.  
  8829.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  8830.     return 0;
  8831.  
  8832.   /* Make it at least twice as big as it is now.  */
  8833.   obuf->length *= 2;
  8834.   /* Make it have at least 150% of the free space we will need.  */
  8835.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  8836.   if (minsize > obuf->length)
  8837.     obuf->length = minsize;
  8838.  
  8839.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  8840.     memory_full ();
  8841.  
  8842.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  8843.   obuf->buf = p;
  8844.  
  8845.   return 0;
  8846. }
  8847.  
  8848. /* Symbol table for macro names and special symbols */
  8849.  
  8850. /*
  8851.  * install a name in the main hash table, even if it is already there.
  8852.  *   name stops with first non alphanumeric, except leading '#'.
  8853.  * caller must check against redefinition if that is desired.
  8854.  * delete_macro () removes things installed by install () in fifo order.
  8855.  * this is important because of the `defined' special symbol used
  8856.  * in #if, and also if pushdef/popdef directives are ever implemented.
  8857.  *
  8858.  * If LEN is >= 0, it is the length of the name.
  8859.  * Otherwise, compute the length by scanning the entire name.
  8860.  *
  8861.  * If HASH is >= 0, it is the precomputed hash code.
  8862.  * Otherwise, compute the hash code.
  8863.  */
  8864. static HASHNODE *
  8865. install (name, len, type, ivalue, value, hash)
  8866.      U_CHAR *name;
  8867.      int len;
  8868.      enum node_type type;
  8869.      int ivalue;
  8870.      char *value;
  8871.      int hash;
  8872. {
  8873.   register HASHNODE *hp;
  8874.   register int i, bucket;
  8875.   register U_CHAR *p, *q;
  8876.  
  8877.   if (len < 0) {
  8878.     p = name;
  8879.     while (is_idchar[*p])
  8880.       p++;
  8881.     len = p - name;
  8882.   }
  8883.  
  8884.   if (hash < 0)
  8885.     hash = hashf (name, len, HASHSIZE);
  8886.  
  8887.   i = sizeof (HASHNODE) + len + 1;
  8888.   hp = (HASHNODE *) xmalloc (i);
  8889.   bucket = hash;
  8890.   hp->bucket_hdr = &hashtab[bucket];
  8891.   hp->next = hashtab[bucket];
  8892.   hashtab[bucket] = hp;
  8893.   hp->prev = NULL;
  8894.   if (hp->next != NULL)
  8895.     hp->next->prev = hp;
  8896.   hp->type = type;
  8897.   hp->length = len;
  8898.   if (hp->type == T_CONST)
  8899.     hp->value.ival = ivalue;
  8900.   else
  8901.     hp->value.cpval = value;
  8902.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  8903.   p = hp->name;
  8904.   q = name;
  8905.   for (i = 0; i < len; i++)
  8906.     *p++ = *q++;
  8907.   hp->name[len] = 0;
  8908.   return hp;
  8909. }
  8910.  
  8911. /*
  8912.  * find the most recent hash node for name name (ending with first
  8913.  * non-identifier char) installed by install
  8914.  *
  8915.  * If LEN is >= 0, it is the length of the name.
  8916.  * Otherwise, compute the length by scanning the entire name.
  8917.  *
  8918.  * If HASH is >= 0, it is the precomputed hash code.
  8919.  * Otherwise, compute the hash code.
  8920.  */
  8921. HASHNODE *
  8922. lookup (name, len, hash)
  8923.      U_CHAR *name;
  8924.      int len;
  8925.      int hash;
  8926. {
  8927.   register U_CHAR *bp;
  8928.   register HASHNODE *bucket;
  8929.  
  8930.   if (len < 0) {
  8931.     for (bp = name; is_idchar[*bp]; bp++) ;
  8932.     len = bp - name;
  8933.   }
  8934.  
  8935.   if (hash < 0)
  8936.     hash = hashf (name, len, HASHSIZE);
  8937.  
  8938.   bucket = hashtab[hash];
  8939.   while (bucket) {
  8940.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  8941.       return bucket;
  8942.     bucket = bucket->next;
  8943.   }
  8944.   return NULL;
  8945. }
  8946.  
  8947. /*
  8948.  * Delete a hash node.  Some weirdness to free junk from macros.
  8949.  * More such weirdness will have to be added if you define more hash
  8950.  * types that need it.
  8951.  */
  8952.  
  8953. /* Note that the DEFINITION of a macro is removed from the hash table
  8954.    but its storage is not freed.  This would be a storage leak
  8955.    except that it is not reasonable to keep undefining and redefining
  8956.    large numbers of macros many times.
  8957.    In any case, this is necessary, because a macro can be #undef'd
  8958.    in the middle of reading the arguments to a call to it.
  8959.    If #undef freed the DEFINITION, that would crash.  */
  8960.  
  8961. static void
  8962. delete_macro (hp)
  8963.      HASHNODE *hp;
  8964. {
  8965.  
  8966.   if (hp->prev != NULL)
  8967.     hp->prev->next = hp->next;
  8968.   if (hp->next != NULL)
  8969.     hp->next->prev = hp->prev;
  8970.  
  8971.   /* make sure that the bucket chain header that
  8972.      the deleted guy was on points to the right thing afterwards. */
  8973.   if (hp == *hp->bucket_hdr)
  8974.     *hp->bucket_hdr = hp->next;
  8975.  
  8976. #if 0
  8977.   if (hp->type == T_MACRO) {
  8978.     DEFINITION *d = hp->value.defn;
  8979.     struct reflist *ap, *nextap;
  8980.  
  8981.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  8982.       nextap = ap->next;
  8983.       free (ap);
  8984.     }
  8985.     free (d);
  8986.   }
  8987. #endif
  8988.   free (hp);
  8989. }
  8990.  
  8991. /*
  8992.  * return hash function on name.  must be compatible with the one
  8993.  * computed a step at a time, elsewhere
  8994.  */
  8995. static int
  8996. hashf (name, len, hashsize)
  8997.      register U_CHAR *name;
  8998.      register int len;
  8999.      int hashsize;
  9000. {
  9001.   register int r = 0;
  9002.  
  9003.   while (len--)
  9004.     r = HASHSTEP (r, *name++);
  9005.  
  9006.   return MAKE_POS (r) % hashsize;
  9007. }
  9008.  
  9009.  
  9010. /* Dump the definition of a single macro HP to OF.  */
  9011. static void
  9012. dump_single_macro (hp, of)
  9013.      register HASHNODE *hp;
  9014.      FILE *of;
  9015. {
  9016.   register DEFINITION *defn = hp->value.defn;
  9017.   struct reflist *ap;
  9018.   int offset;
  9019.   int concat;
  9020.  
  9021.  
  9022.   /* Print the definition of the macro HP.  */
  9023.  
  9024.   fprintf (of, "#define %s", hp->name);
  9025.  
  9026.   if (defn->nargs >= 0) {
  9027.     int i;
  9028.  
  9029.     fprintf (of, "(");
  9030.     for (i = 0; i < defn->nargs; i++) {
  9031.       dump_arg_n (defn, i, of);
  9032.       if (i + 1 < defn->nargs)
  9033.     fprintf (of, ", ");
  9034.     }
  9035.     fprintf (of, ")");
  9036.   }
  9037.  
  9038.   fprintf (of, " ");
  9039.  
  9040.   offset = 0;
  9041.   concat = 0;
  9042.   for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  9043.     dump_defn_1 (defn->expansion, offset, ap->nchars, of);
  9044.     offset += ap->nchars;
  9045.     if (!traditional) {
  9046.       if (ap->nchars != 0)
  9047.     concat = 0;
  9048.       if (ap->stringify)
  9049.     fprintf (of, " #");
  9050.       if (ap->raw_before && !concat)
  9051.     fprintf (of, " ## ");
  9052.       concat = 0;
  9053.     }
  9054.     dump_arg_n (defn, ap->argno, of);
  9055.     if (!traditional && ap->raw_after) {
  9056.       fprintf (of, " ## ");
  9057.       concat = 1;
  9058.     }
  9059.   }
  9060.   dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
  9061.   fprintf (of, "\n");
  9062. }
  9063.  
  9064. /* Dump all macro definitions as #defines to stdout.  */
  9065.  
  9066. static void
  9067. dump_all_macros ()
  9068. {
  9069.   int bucket;
  9070.  
  9071.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  9072.     register HASHNODE *hp;
  9073.  
  9074.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  9075.       if (hp->type == T_MACRO)
  9076.     dump_single_macro (hp, stdout);
  9077.     }
  9078.   }
  9079. }
  9080.  
  9081. /* Output to OF a substring of a macro definition.
  9082.    BASE is the beginning of the definition.
  9083.    Output characters START thru LENGTH.
  9084.    Unless traditional, discard newlines outside of strings, thus
  9085.    converting funny-space markers to ordinary spaces.  */
  9086.  
  9087. static void
  9088. dump_defn_1 (base, start, length, of)
  9089.      U_CHAR *base;
  9090.      int start;
  9091.      int length;
  9092.      FILE *of;
  9093. {
  9094.   U_CHAR *p = base + start;
  9095.   U_CHAR *limit = base + start + length;
  9096.  
  9097.   if (traditional)
  9098.     fwrite (p, sizeof (*p), length, of);
  9099.   else {
  9100.     while (p < limit) {
  9101.       if (*p == '\"' || *p =='\'') {
  9102.     U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
  9103.                      NULL_PTR, NULL_PTR);
  9104.     fwrite (p, sizeof (*p), p1 - p, of);
  9105.     p = p1;
  9106.       } else {
  9107.     if (*p != '\n')
  9108.       putc (*p, of);
  9109.     p++;
  9110.       }
  9111.     }
  9112.   }
  9113. }
  9114.  
  9115. /* Print the name of argument number ARGNUM of macro definition DEFN
  9116.    to OF.
  9117.    Recall that DEFN->args.argnames contains all the arg names
  9118.    concatenated in reverse order with comma-space in between.  */
  9119.  
  9120. static void
  9121. dump_arg_n (defn, argnum, of)
  9122.      DEFINITION *defn;
  9123.      int argnum;
  9124.      FILE *of;
  9125. {
  9126.   register U_CHAR *p = defn->args.argnames;
  9127.   while (argnum + 1 < defn->nargs) {
  9128.     p = (U_CHAR *) index (p, ' ') + 1;
  9129.     argnum++;
  9130.   }
  9131.  
  9132.   while (*p && *p != ',') {
  9133.     putc (*p, of);
  9134.     p++;
  9135.   }
  9136. }
  9137.  
  9138. /* Initialize syntactic classifications of characters.  */
  9139.  
  9140. static void
  9141. initialize_char_syntax ()
  9142. {
  9143.   register int i;
  9144.  
  9145.   /*
  9146.    * Set up is_idchar and is_idstart tables.  These should be
  9147.    * faster than saying (is_alpha (c) || c == '_'), etc.
  9148.    * Set up these things before calling any routines tthat
  9149.    * refer to them.
  9150.    */
  9151.   for (i = 'a'; i <= 'z'; i++) {
  9152.     is_idchar[i - 'a' + 'A'] = 1;
  9153.     is_idchar[i] = 1;
  9154.     is_idstart[i - 'a' + 'A'] = 1;
  9155.     is_idstart[i] = 1;
  9156.   }
  9157.   for (i = '0'; i <= '9'; i++)
  9158.     is_idchar[i] = 1;
  9159.   is_idchar['_'] = 1;
  9160.   is_idstart['_'] = 1;
  9161.   is_idchar['$'] = dollars_in_ident;
  9162.   is_idstart['$'] = dollars_in_ident;
  9163.  
  9164.   /* horizontal space table */
  9165.   is_hor_space[' '] = 1;
  9166.   is_hor_space['\t'] = 1;
  9167.   is_hor_space['\v'] = 1;
  9168.   is_hor_space['\f'] = 1;
  9169.   is_hor_space['\r'] = 1;
  9170.  
  9171.   is_space[' '] = 1;
  9172.   is_space['\t'] = 1;
  9173.   is_space['\v'] = 1;
  9174.   is_space['\f'] = 1;
  9175.   is_space['\n'] = 1;
  9176.   is_space['\r'] = 1;
  9177. }
  9178.  
  9179. /* Initialize the built-in macros.  */
  9180.  
  9181. static void
  9182. initialize_builtins (inp, outp)
  9183.      FILE_BUF *inp;
  9184.      FILE_BUF *outp;
  9185. {
  9186.   install ("__LINE__", -1, T_SPECLINE, 0, NULL_PTR, -1);
  9187.   install ("__DATE__", -1, T_DATE, 0, NULL_PTR, -1);
  9188.   install ("__FILE__", -1, T_FILE, 0, NULL_PTR, -1);
  9189.   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, NULL_PTR, -1);
  9190.   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, NULL_PTR, -1);
  9191.   install ("__VERSION__", -1, T_VERSION, 0, NULL_PTR, -1);
  9192. #ifndef NO_BUILTIN_SIZE_TYPE
  9193.   install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, NULL_PTR, -1);
  9194. #endif
  9195. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  9196.   install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, NULL_PTR, -1);
  9197. #endif
  9198.   install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, NULL_PTR, -1);
  9199.   install ("__USER_LABEL_PREFIX__",-1,T_USER_LABEL_PREFIX_TYPE,0,NULL_PTR, -1);
  9200.   install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, NULL_PTR, -1);
  9201.   install ("__TIME__", -1, T_TIME, 0, NULL_PTR, -1);
  9202.   if (!traditional)
  9203.     install ("__STDC__", -1, T_CONST, STDC_VALUE, NULL_PTR, -1);
  9204.   if (objc)
  9205.     install ("__OBJC__", -1, T_CONST, 1, NULL_PTR, -1);
  9206. /*  This is supplied using a -D by the compiler driver
  9207.     so that it is present only when truly compiling with GNU C.  */
  9208. /*  install ("__GNUC__", -1, T_CONST, 2, NULL_PTR, -1);  */
  9209.  
  9210.   if (debug_output)
  9211.     {
  9212.       char directive[2048];
  9213.       register struct directive *dp = &directive_table[0];
  9214.       struct tm *timebuf = timestamp ();
  9215.  
  9216.       sprintf (directive, " __BASE_FILE__ \"%s\"\n",
  9217.            instack[0].nominal_fname);
  9218.       output_line_command (inp, outp, 0, same_file);
  9219.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9220.  
  9221.       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
  9222.       output_line_command (inp, outp, 0, same_file);
  9223.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9224.  
  9225. #ifndef NO_BUILTIN_SIZE_TYPE
  9226.       sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
  9227.       output_line_command (inp, outp, 0, same_file);
  9228.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9229. #endif
  9230.  
  9231. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  9232.       sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
  9233.       output_line_command (inp, outp, 0, same_file);
  9234.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9235. #endif
  9236.  
  9237.       sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
  9238.       output_line_command (inp, outp, 0, same_file);
  9239.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9240.  
  9241.       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
  9242.            monthnames[timebuf->tm_mon],
  9243.            timebuf->tm_mday, timebuf->tm_year + 1900);
  9244.       output_line_command (inp, outp, 0, same_file);
  9245.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9246.  
  9247.       sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
  9248.            timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
  9249.       output_line_command (inp, outp, 0, same_file);
  9250.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9251.  
  9252.       if (!traditional)
  9253.     {
  9254.           sprintf (directive, " __STDC__ 1");
  9255.           output_line_command (inp, outp, 0, same_file);
  9256.           pass_thru_directive (directive, &directive[strlen (directive)],
  9257.                    outp, dp);
  9258.     }
  9259.       if (objc)
  9260.     {
  9261.           sprintf (directive, " __OBJC__ 1");
  9262.           output_line_command (inp, outp, 0, same_file);
  9263.           pass_thru_directive (directive, &directive[strlen (directive)],
  9264.                    outp, dp);
  9265.     }
  9266.     }
  9267. }
  9268.  
  9269. /*
  9270.  * process a given definition string, for initialization
  9271.  * If STR is just an identifier, define it with value 1.
  9272.  * If STR has anything after the identifier, then it should
  9273.  * be identifier=definition.
  9274.  */
  9275.  
  9276. static void
  9277. make_definition (str, op)
  9278.      U_CHAR *str;
  9279.      FILE_BUF *op;
  9280. {
  9281.   FILE_BUF *ip;
  9282.   struct directive *kt;
  9283.   U_CHAR *buf, *p;
  9284.  
  9285.   buf = str;
  9286.   p = str;
  9287.   if (!is_idstart[*p]) {
  9288.     error ("malformed option `-D %s'", str);
  9289.     return;
  9290.   }
  9291.   while (is_idchar[*++p])
  9292.     ;
  9293.   if (*p == '(') {
  9294.     while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
  9295.       ;
  9296.     if (*p++ != ')')
  9297.       p = str;            /* Error */
  9298.   }
  9299.   if (*p == 0) {
  9300.     buf = (U_CHAR *) alloca (p - buf + 4);
  9301.     strcpy ((char *)buf, str);
  9302.     strcat ((char *)buf, " 1");
  9303.   } else if (*p != '=') {
  9304.     error ("malformed option `-D %s'", str);
  9305.     return;
  9306.   } else {
  9307.     U_CHAR *q;
  9308.     /* Copy the entire option so we can modify it.  */
  9309.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  9310.     strncpy (buf, str, p - str);
  9311.     /* Change the = to a space.  */
  9312.     buf[p - str] = ' ';
  9313.     /* Scan for any backslash-newline and remove it.  */
  9314.     p++;
  9315.     q = &buf[p - str];
  9316.     while (*p) {
  9317.       if (*p == '\"' || *p == '\'') {
  9318.     int unterminated = 0;
  9319.     U_CHAR *p1 = skip_quoted_string (p, p + strlen (p), 0,
  9320.                      NULL_PTR, NULL_PTR, &unterminated);
  9321.     if (unterminated)
  9322.       return;
  9323.     while (p != p1)
  9324.       if (*p == '\\' && p[1] == '\n')
  9325.         p += 2;
  9326.       else
  9327.         *q++ = *p++;
  9328.       } else if (*p == '\\' && p[1] == '\n')
  9329.     p += 2;
  9330.       /* Change newline chars into newline-markers.  */
  9331.       else if (*p == '\n')
  9332.     {
  9333.       *q++ = '\n';
  9334.       *q++ = '\n';
  9335.       p++;
  9336.     }
  9337.       else
  9338.     *q++ = *p++;
  9339.     }
  9340.     *q = 0;
  9341.   }
  9342.   
  9343.   ip = &instack[++indepth];
  9344.   ip->nominal_fname = ip->fname = "*Initialization*";
  9345.  
  9346.   ip->buf = ip->bufp = buf;
  9347.   ip->length = strlen (buf);
  9348.   ip->lineno = 1;
  9349.   ip->macro = 0;
  9350.   ip->free_ptr = 0;
  9351.   ip->if_stack = if_stack;
  9352.   ip->system_header_p = 0;
  9353.  
  9354.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  9355.     ;
  9356.  
  9357.   /* Pass NULL instead of OP, since this is a "predefined" macro.  */
  9358.   do_define (buf, buf + strlen (buf), NULL_PTR, kt);
  9359.   --indepth;
  9360. }
  9361.  
  9362. /* JF, this does the work for the -U option */
  9363.  
  9364. static void
  9365. make_undef (str, op)
  9366.      U_CHAR *str;
  9367.      FILE_BUF *op;
  9368. {
  9369.   FILE_BUF *ip;
  9370.   struct directive *kt;
  9371.  
  9372.   ip = &instack[++indepth];
  9373.   ip->nominal_fname = ip->fname = "*undef*";
  9374.  
  9375.   ip->buf = ip->bufp = str;
  9376.   ip->length = strlen (str);
  9377.   ip->lineno = 1;
  9378.   ip->macro = 0;
  9379.   ip->free_ptr = 0;
  9380.   ip->if_stack = if_stack;
  9381.   ip->system_header_p = 0;
  9382.  
  9383.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  9384.     ;
  9385.  
  9386.   do_undef (str, str + strlen (str), op, kt);
  9387.   --indepth;
  9388. }
  9389.  
  9390. /* Process the string STR as if it appeared as the body of a #assert.
  9391.    OPTION is the option name for which STR was the argument.  */
  9392.  
  9393. static void
  9394. make_assertion (option, str)
  9395.      char *option;
  9396.      U_CHAR *str;
  9397. {
  9398.   FILE_BUF *ip;
  9399.   struct directive *kt;
  9400.   U_CHAR *buf, *p, *q;
  9401.  
  9402.   /* Copy the entire option so we can modify it.  */
  9403.   buf = (U_CHAR *) alloca (strlen (str) + 1);
  9404.   strcpy ((char *) buf, str);
  9405.   /* Scan for any backslash-newline and remove it.  */
  9406.   p = q = buf;
  9407.   while (*p) {
  9408.     if (*p == '\\' && p[1] == '\n')
  9409.       p += 2;
  9410.     else
  9411.       *q++ = *p++;
  9412.   }
  9413.   *q = 0;
  9414.  
  9415.   p = buf;
  9416.   if (!is_idstart[*p]) {
  9417.     error ("malformed option `%s %s'", option, str);
  9418.     return;
  9419.   }
  9420.   while (is_idchar[*++p])
  9421.     ;
  9422.   while (*p == ' ' || *p == '\t') p++;
  9423.   if (! (*p == 0 || *p == '(')) {
  9424.     error ("malformed option `%s %s'", option, str);
  9425.     return;
  9426.   }
  9427.   
  9428.   ip = &instack[++indepth];
  9429.   ip->nominal_fname = ip->fname = "*Initialization*";
  9430.  
  9431.   ip->buf = ip->bufp = buf;
  9432.   ip->length = strlen (buf);
  9433.   ip->lineno = 1;
  9434.   ip->macro = 0;
  9435.   ip->free_ptr = 0;
  9436.   ip->if_stack = if_stack;
  9437.   ip->system_header_p = 0;
  9438.  
  9439.   for (kt = directive_table; kt->type != T_ASSERT; kt++)
  9440.     ;
  9441.  
  9442.   /* pass NULL as output ptr to do_define since we KNOW it never
  9443.      does any output.... */
  9444.   do_assert (buf, buf + strlen (buf) , NULL_PTR, kt);
  9445.   --indepth;
  9446. }
  9447.  
  9448. /* Append a chain of `struct file_name_list's
  9449.    to the end of the main include chain.
  9450.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  9451.  
  9452. static void
  9453. append_include_chain (first, last)
  9454.      struct file_name_list *first, *last;
  9455. {
  9456.   struct file_name_list *dir;
  9457.  
  9458.   if (!first || !last)
  9459.     return;
  9460.  
  9461.   if (include == 0)
  9462.     include = first;
  9463.   else
  9464.     last_include->next = first;
  9465.  
  9466.   if (first_bracket_include == 0)
  9467.     first_bracket_include = first;
  9468.  
  9469.   for (dir = first; ; dir = dir->next) {
  9470.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  9471.     if (len > max_include_len)
  9472.       max_include_len = len;
  9473.     if (dir == last)
  9474.       break;
  9475.   }
  9476.  
  9477.   last->next = NULL;
  9478.   last_include = last;
  9479. }
  9480.  
  9481. /* Add output to `deps_buffer' for the -M switch.
  9482.    STRING points to the text to be output.
  9483.    SPACER is ':' for targets, ' ' for dependencies, zero for text
  9484.    to be inserted literally.  */
  9485.  
  9486. static void
  9487. deps_output (string, spacer)
  9488.      char *string;
  9489.      int spacer;
  9490. {
  9491.   int size = strlen (string);
  9492.  
  9493.   if (size == 0)
  9494.     return;
  9495.  
  9496. #ifndef MAX_OUTPUT_COLUMNS
  9497. #define MAX_OUTPUT_COLUMNS 72
  9498. #endif
  9499.   if (spacer
  9500.       && deps_column > 0
  9501.       && (deps_column + size) > MAX_OUTPUT_COLUMNS)
  9502.   {
  9503.     deps_output (" \\\n  ", 0);
  9504.     deps_column = 0;
  9505.   }
  9506.  
  9507.   if (deps_size + size + 8 > deps_allocated_size) {
  9508.     deps_allocated_size = (deps_size + size + 50) * 2;
  9509.     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
  9510.   }
  9511.   if (spacer == ' ' && deps_column > 0)
  9512.     deps_buffer[deps_size++] = ' ';
  9513.   bcopy (string, &deps_buffer[deps_size], size);
  9514.   deps_size += size;
  9515.   deps_column += size;
  9516.   if (spacer == ':')
  9517.     deps_buffer[deps_size++] = ':';
  9518.   deps_buffer[deps_size] = 0;
  9519. }
  9520.  
  9521. #if defined(USG) || defined(VMS)
  9522. #ifndef BSTRING
  9523.  
  9524. void
  9525. bzero (b, length)
  9526.      register char *b;
  9527.      register unsigned length;
  9528. {
  9529.   while (length-- > 0)
  9530.     *b++ = 0;
  9531. }
  9532.  
  9533. void
  9534. bcopy (b1, b2, length)
  9535.      register char *b1;
  9536.      register char *b2;
  9537.      register unsigned length;
  9538. {
  9539.   while (length-- > 0)
  9540.     *b2++ = *b1++;
  9541. }
  9542.  
  9543. int
  9544. bcmp (b1, b2, length)    /* This could be a macro! */
  9545.      register char *b1;
  9546.      register char *b2;
  9547.      register unsigned length;
  9548. {
  9549.    while (length-- > 0)
  9550.      if (*b1++ != *b2++)
  9551.        return 1;
  9552.  
  9553.    return 0;
  9554. }
  9555. #endif /* not BSTRING */
  9556. #endif /* USG or VMS */
  9557.  
  9558.  
  9559. static void
  9560. fatal (str, arg)
  9561.      char *str, *arg;
  9562. {
  9563.   fprintf (stderr, "%s: ", progname);
  9564.   fprintf (stderr, str, arg);
  9565.   fprintf (stderr, "\n");
  9566.   exit (FAILURE_EXIT_CODE);
  9567. }
  9568.  
  9569. /* More 'friendly' abort that prints the line and file.
  9570.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  9571.  
  9572. void
  9573. fancy_abort ()
  9574. {
  9575.   fatal ("Internal gcc abort.");
  9576. }
  9577.  
  9578. static void
  9579. perror_with_name (name)
  9580.      char *name;
  9581. {
  9582.   fprintf (stderr, "%s: ", progname);
  9583.   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
  9584.   errors++;
  9585. }
  9586.  
  9587. static void
  9588. pfatal_with_name (name)
  9589.      char *name;
  9590. {
  9591.   perror_with_name (name);
  9592. #ifdef VMS
  9593.   exit (vaxc$errno);
  9594. #else
  9595.   exit (FAILURE_EXIT_CODE);
  9596. #endif
  9597. }
  9598.  
  9599. /* Handler for SIGPIPE.  */
  9600.  
  9601. static void
  9602. pipe_closed (signo)
  9603.      /* If this is missing, some compilers complain.  */
  9604.      int signo;
  9605. {
  9606.   fatal ("output pipe has been closed");
  9607. }
  9608.  
  9609. static void
  9610. memory_full ()
  9611. {
  9612.   fatal ("Memory exhausted.");
  9613. }
  9614.  
  9615.  
  9616. char *
  9617. xmalloc (size)
  9618.      unsigned size;
  9619. {
  9620.   register char *ptr = (char *) malloc (size);
  9621.   if (ptr != 0) return (ptr);
  9622.   memory_full ();
  9623.   /*NOTREACHED*/
  9624.   return 0;
  9625. }
  9626.  
  9627. static char *
  9628. xrealloc (old, size)
  9629.      char *old;
  9630.      unsigned size;
  9631. {
  9632.   register char *ptr = (char *) realloc (old, size);
  9633.   if (ptr != 0) return (ptr);
  9634.   memory_full ();
  9635.   /*NOTREACHED*/
  9636.   return 0;
  9637. }
  9638.  
  9639. static char *
  9640. xcalloc (number, size)
  9641.      unsigned number, size;
  9642. {
  9643.   register unsigned total = number * size;
  9644.   register char *ptr = (char *) malloc (total);
  9645.   if (ptr != 0) {
  9646.     if (total > 100)
  9647.       bzero (ptr, total);
  9648.     else {
  9649.       /* It's not too long, so loop, zeroing by longs.
  9650.      It must be safe because malloc values are always well aligned.  */
  9651.       register long *zp = (long *) ptr;
  9652.       register long *zl = (long *) (ptr + total - 4);
  9653.       register int i = total - 4;
  9654.       while (zp < zl)
  9655.     *zp++ = 0;
  9656.       if (i < 0)
  9657.     i = 0;
  9658.       while (i < total)
  9659.     ptr[i++] = 0;
  9660.     }
  9661.     return ptr;
  9662.   }
  9663.   memory_full ();
  9664.   /*NOTREACHED*/
  9665.   return 0;
  9666. }
  9667.  
  9668. static char *
  9669. savestring (input)
  9670.      char *input;
  9671. {
  9672.   unsigned size = strlen (input);
  9673.   char *output = xmalloc (size + 1);
  9674.   strcpy (output, input);
  9675.   return output;
  9676. }
  9677.  
  9678. /* Get the file-mode and data size of the file open on FD
  9679.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  9680.  
  9681. static int
  9682. file_size_and_mode (fd, mode_pointer, size_pointer)
  9683.      int fd;
  9684.      int *mode_pointer;
  9685.      long int *size_pointer;
  9686. {
  9687.   struct stat sbuf;
  9688.  
  9689.   if (fstat (fd, &sbuf) < 0) return (-1);
  9690.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  9691.   if (size_pointer) *size_pointer = sbuf.st_size;
  9692.   return 0;
  9693. }
  9694.  
  9695. static void
  9696. output_dots (fd, depth)
  9697.      FILE* fd;
  9698.      int depth;
  9699. {
  9700.   while (depth > 0) {
  9701.     putc ('.', fd);
  9702.     depth--;
  9703.   }
  9704. }
  9705.   
  9706.  
  9707. #ifdef VMS
  9708.  
  9709. /* Under VMS we need to fix up the "include" specification
  9710.    filename so that everything following the 1st slash is
  9711.    changed into its correct VMS file specification. */
  9712.  
  9713. static void
  9714. hack_vms_include_specification (fname)
  9715.      char *fname;
  9716. {
  9717.   register char *cp, *cp1, *cp2;
  9718.   int f, check_filename_before_returning, no_prefix_seen;
  9719.   char Local[512];
  9720.  
  9721.   check_filename_before_returning = 0;
  9722.   no_prefix_seen = 0;
  9723.  
  9724.   /* Ignore leading "./"s */
  9725.   while (fname[0] == '.' && fname[1] == '/') {
  9726.     strcpy (fname, fname+2);
  9727.     no_prefix_seen = 1;        /* mark this for later */
  9728.   }
  9729.   /* Look for the boundary between the VMS and UNIX filespecs */
  9730.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  9731.   if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto            */
  9732.   if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
  9733.   if (cp) {
  9734.     cp++;
  9735.   } else {
  9736.     cp = index (fname, '/');    /* Look for the "/" */
  9737.   }
  9738.  
  9739.   /*
  9740.    * Check if we have a vax-c style '#include filename'
  9741.    * and add the missing .h
  9742.    */
  9743.   if (cp == 0) {
  9744.     if (index(fname,'.') == 0)
  9745.       strcat(fname, ".h");
  9746.   } else {
  9747.     if (index(cp,'.') == 0)
  9748.       strcat(cp, ".h");
  9749.   }
  9750.  
  9751.   cp2 = Local;            /* initialize */
  9752.  
  9753.   /* We are trying to do a number of things here.  First of all, we are
  9754.      trying to hammer the filenames into a standard format, such that later
  9755.      processing can handle them.
  9756.      
  9757.      If the file name contains something like [dir.], then it recognizes this
  9758.      as a root, and strips the ".]".  Later processing will add whatever is
  9759.      needed to get things working properly.
  9760.      
  9761.      If no device is specified, then the first directory name is taken to be
  9762.      a device name (or a rooted logical). */
  9763.  
  9764.   /* See if we found that 1st slash */
  9765.   if (cp == 0) return;        /* Nothing to do!!! */
  9766.   if (*cp != '/') return;    /* Nothing to do!!! */
  9767.   /* Point to the UNIX filename part (which needs to be fixed!) */
  9768.   cp1 = cp+1;
  9769.   /* If the directory spec is not rooted, we can just copy
  9770.      the UNIX filename part and we are done */
  9771.   if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
  9772.     if (cp[-2] != '.') {
  9773.       /*
  9774.        * The VMS part ends in a `]', and the preceding character is not a `.'.
  9775.        * We strip the `]', and then splice the two parts of the name in the
  9776.        * usual way.  Given the default locations for include files in cccp.c,
  9777.        * we will only use this code if the user specifies alternate locations
  9778.        * with the /include (-I) switch on the command line.  */
  9779.       cp -= 1;            /* Strip "]" */
  9780.       cp1--;            /* backspace */
  9781.     } else {
  9782.       /*
  9783.        * The VMS part has a ".]" at the end, and this will not do.  Later
  9784.        * processing will add a second directory spec, and this would be a syntax
  9785.        * error.  Thus we strip the ".]", and thus merge the directory specs.
  9786.        * We also backspace cp1, so that it points to a '/'.  This inhibits the
  9787.        * generation of the 000000 root directory spec (which does not belong here
  9788.        * in this case).
  9789.        */
  9790.       cp -= 2;            /* Strip ".]" */
  9791.       cp1--; };            /* backspace */
  9792.   } else {
  9793.  
  9794.     /* We drop in here if there is no VMS style directory specification yet.
  9795.      * If there is no device specification either, we make the first dir a
  9796.      * device and try that.  If we do not do this, then we will be essentially
  9797.      * searching the users default directory (as if they did a #include "asdf.h").
  9798.      *
  9799.      * Then all we need to do is to push a '[' into the output string. Later
  9800.      * processing will fill this in, and close the bracket.
  9801.      */
  9802.     if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec.  take first dir */
  9803.     *cp2++ = '[';        /* Open the directory specification */
  9804.   }
  9805.  
  9806.   /* at this point we assume that we have the device spec, and (at least
  9807.      the opening "[" for a directory specification.  We may have directories
  9808.      specified already */
  9809.  
  9810.   /* If there are no other slashes then the filename will be
  9811.      in the "root" directory.  Otherwise, we need to add
  9812.      directory specifications. */
  9813.   if (index (cp1, '/') == 0) {
  9814.     /* Just add "000000]" as the directory string */
  9815.     strcpy (cp2, "000000]");
  9816.     cp2 += strlen (cp2);
  9817.     check_filename_before_returning = 1; /* we might need to fool with this later */
  9818.   } else {
  9819.     /* As long as there are still subdirectories to add, do them. */
  9820.     while (index (cp1, '/') != 0) {
  9821.       /* If this token is "." we can ignore it */
  9822.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  9823.     cp1 += 2;
  9824.     continue;
  9825.       }
  9826.       /* Add a subdirectory spec. Do not duplicate "." */
  9827.       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
  9828.     *cp2++ = '.';
  9829.       /* If this is ".." then the spec becomes "-" */
  9830.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  9831.     /* Add "-" and skip the ".." */
  9832.     *cp2++ = '-';
  9833.     cp1 += 3;
  9834.     continue;
  9835.       }
  9836.       /* Copy the subdirectory */
  9837.       while (*cp1 != '/') *cp2++= *cp1++;
  9838.       cp1++;            /* Skip the "/" */
  9839.     }
  9840.     /* Close the directory specification */
  9841.     if (cp2[-1] == '.')        /* no trailing periods */
  9842.       cp2--;
  9843.     *cp2++ = ']';
  9844.   }
  9845.   /* Now add the filename */
  9846.   while (*cp1) *cp2++ = *cp1++;
  9847.   *cp2 = 0;
  9848.   /* Now append it to the original VMS spec. */
  9849.   strcpy (cp, Local);
  9850.  
  9851.   /* If we put a [000000] in the filename, try to open it first. If this fails,
  9852.      remove the [000000], and return that name.  This provides flexibility
  9853.      to the user in that they can use both rooted and non-rooted logical names
  9854.      to point to the location of the file.  */
  9855.  
  9856.   if (check_filename_before_returning && no_prefix_seen) {
  9857.     f = open (fname, O_RDONLY, 0666);
  9858.     if (f >= 0) {
  9859.       /* The file name is OK as it is, so return it as is.  */
  9860.       close (f);
  9861.       return;
  9862.     }
  9863.     /* The filename did not work.  Try to remove the [000000] from the name,
  9864.        and return it.  */
  9865.     cp = index (fname, '[');
  9866.     cp2 = index (fname, ']') + 1;
  9867.     strcpy (cp, cp2);        /* this gets rid of it */
  9868.   }
  9869.   return;
  9870. }
  9871. #endif    /* VMS */
  9872.  
  9873. #ifdef    VMS
  9874.  
  9875. /* These are the read/write replacement routines for
  9876.    VAX-11 "C".  They make read/write behave enough
  9877.    like their UNIX counterparts that CCCP will work */
  9878.  
  9879. static int
  9880. read (fd, buf, size)
  9881.      int fd;
  9882.      char *buf;
  9883.      int size;
  9884. {
  9885. #undef    read    /* Get back the REAL read routine */
  9886.   register int i;
  9887.   register int total = 0;
  9888.  
  9889.   /* Read until the buffer is exhausted */
  9890.   while (size > 0) {
  9891.     /* Limit each read to 32KB */
  9892.     i = (size > (32*1024)) ? (32*1024) : size;
  9893.     i = read (fd, buf, i);
  9894.     if (i <= 0) {
  9895.       if (i == 0) return (total);
  9896.       return (i);
  9897.     }
  9898.     /* Account for this read */
  9899.     total += i;
  9900.     buf += i;
  9901.     size -= i;
  9902.   }
  9903.   return (total);
  9904. }
  9905.  
  9906. static int
  9907. write (fd, buf, size)
  9908.      int fd;
  9909.      char *buf;
  9910.      int size;
  9911. {
  9912. #undef    write    /* Get back the REAL write routine */
  9913.   int i;
  9914.   int j;
  9915.  
  9916.   /* Limit individual writes to 32Kb */
  9917.   i = size;
  9918.   while (i > 0) {
  9919.     j = (i > (32*1024)) ? (32*1024) : i;
  9920.     if (write (fd, buf, j) < 0) return (-1);
  9921.     /* Account for the data written */
  9922.     buf += j;
  9923.     i -= j;
  9924.   }
  9925.   return (size);
  9926. }
  9927.  
  9928. /* The following wrapper functions supply additional arguments to the VMS
  9929.    I/O routines to optimize performance with file handling.  The arguments
  9930.    are:
  9931.      "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
  9932.      "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
  9933.      "fop=tef"- Truncate unused portions of file when closing file.
  9934.      "shr=nil"- Disallow file sharing while file is open.
  9935.  */
  9936.  
  9937. static FILE *
  9938. freopen (fname, type, oldfile)
  9939.      char *fname;
  9940.      char *type;
  9941.      FILE *oldfile;
  9942. {
  9943. #undef    freopen    /* Get back the REAL fopen routine */
  9944.   if (strcmp (type, "w") == 0)
  9945.     return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  9946.   return freopen (fname, type, oldfile, "mbc=16");
  9947. }
  9948.  
  9949. static FILE *
  9950. fopen (fname, type)
  9951.      char *fname;
  9952.      char *type;
  9953. {
  9954. #undef fopen    /* Get back the REAL fopen routine */
  9955.   if (strcmp (type, "w") == 0)
  9956.     return fopen (fname, type, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  9957.   return fopen (fname, type, "mbc=16");
  9958. }
  9959.  
  9960. static int 
  9961. open (fname, flags, prot)
  9962.      char *fname;
  9963.      int flags;
  9964.      int prot;
  9965. {
  9966. #undef open    /* Get back the REAL open routine */
  9967.   return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
  9968. }
  9969.  
  9970. /* Avoid run-time library bug, where copying M out of N+M characters with
  9971.    N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
  9972.    gcc-cpp exercises this particular bug.  */
  9973.  
  9974. static char *
  9975. strncat (dst, src, cnt)
  9976.      char *dst;
  9977.      const char *src;
  9978.      unsigned cnt;
  9979. {
  9980.   register char *d = dst, *s = (char *) src;
  9981.   register int n = cnt;    /* convert to _signed_ type */
  9982.  
  9983.   while (*d) d++;    /* advance to end */
  9984.   while (--n >= 0)
  9985.     if (!(*d++ = *s++)) break;
  9986.   if (n < 0) *d = '\0';
  9987.   return dst;
  9988. }
  9989. #endif /* VMS */
  9990.